Leigh Dodds
Java Syntax Reference
This handout is designed as a reference sheet for common Java syntax. For a more complete reference see The Java Tutorial, or the book “Java in a Nutshell”.
Table of Contents
Operators 2
Assignment 2
Arithmetic 2
Increment and Decrement 2
Boolean 3
Logical 3
Shift 3
Casts 4
Instanceof 4
Flow Of Control 4
If Statement 4
Switch Statement 5
For Loop 6
While Loop 7
Variables 7
Primitive Data Types 7
Declaring a Variable 8
Declaring an Array 8
Declaring Constants 9
Packages 10
Naming 10
Defining a Package 10
Importing a Package 10
Classes and Interfaces 11
Defining a Class 11
Defining an Interface 11
Extending a Class 12
Extending an Interface 12
Implementing an Interface 12
Methods 13
Defining a Method 13
Defining a Method with Parameters 13
Constructors 14
Objects 14
Creating (Instantiating) an Object 14
Modifiers 15
Visibility Modifiers 15
Public Classes and Interfaces 16
Abstract Classes and Methods 16
Final Classes Methods and Variables 17
Static Methods and Variables 17
Exceptions 18
Declaring a method to throw an Exception 18
Throwing an Exception 18
Handling (Catching) an Exception 19
Miscellaneous 19
Comments 19
Keywords 20
Legal Names 20
Operators
Assignment
= ;
Example:
int myNumber;
myNumber = 1;
• is a variable, function, or parameter -- or any combination of them, with operators and parentheses -- that evaluates to the same type as .
• If has been declared final, then can only be assigned a value when it is declared. All subsequent attempts to assign a value to are compile time errors.
Arithmetic
|+ |Addition |
|- |Subtraction |
|/ |Division, ignoring remainder |
|* |Multiplication |
|% |Modulo, remainder of dividing x by y |
Increment and Decrement
|Operator |Description |Example |
|++ |Adds one to the variable and stores and stores|x++ |
| |the new value in that variable | |
|-- |Removes one from the variable and stores the |y-- |
| |new value in that variable | |
|+= |Adds the specified number to the variable and |x += 5 |
| |stores the new value in that variable | |
|-- |Removes the specified number to the variable |y -= 5 |
| |and stores the new value in that variable | |
|*= |Multiplies the variable by the specified |x *= 2 |
| |number and stores the new value in that | |
| |variable | |
|/= |Divides the variable by the specified number |y /= 2 |
| |and stores the integer result in that variable| |
Boolean
|!= |not equal to |
|> |greater than |
|< |less than |
|>= |greater than or equal to |
|> |op1 >> op2 |shift bits of op1 right by distance op2 |
|> |op1 >>> op2 |shift bits of op1 right by distance op2 (unsigned) |
Casts
= ();
Example:
float myFloat;
int myInt;
myFloat = (int)myInt;
• Casting does not change an object, just the type used to reference it.
• Casting from one primitive type to another may cause some loss of precision.
• When casting an object, if the cast is not legal then a ClassCastException is thrown.
Instanceof
The instanceof operator checks whether one object is an instance of another,
instanceof
Example:
myString instanceof String
• An object is an instance of a class if it directly or indirectly descends from that class.
Flow Of Control
If Statement
if( ) {
}
If-Else:
if( ) {
} else {
}
If-Else-If:
if( ) {
} else if( ) {
} else {
}
Examples:
if (x && y) {
/* code if true */
}
if (shoppingCart.isEmpty())
{
/* code is shopping cart empty
}
else
{
/* code if shopping cart is not empty
}
Switch Statement
switch {
case :
;
break;
case :
;
break;
case :
;
break;
default :
break;
}
Example:
switch (shoppingCart.size())
{
case 1:
{
showMessage(“You have one item in your shopping cart”);
break;
}
case 2:
{
showMessage(“You have two items in your shopping cart”);
break;
}
default:
{
showMessage(“You have no items in your shopping cart”);
}
}
• The break statement causes the program to proceed with the first statement after the switch structure. The break statement is used because otherwise the cases of the switch statement would otherwise run together. If break is not used anywhere in a switch structure, then each time a match occurs in the structure, the statements for all the remaining cases will be executed (or until a break is encountered).
• The for a select statement must be an integer, char, or an enumerated type.
• If the does not match any of the values, then the default statement is executed.
For Loop
for (; ; ) {
/* Java statements */
}
Example:
//count from one to ten
for (int i=1; i ................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.