Java Reference
In-Depth Information
program state in some way. Any expression with side effects can be used as a state‐
ment simply by following it with a semicolon. The legal types of expression state‐
ments are assignments, increments and decrements, method calls, and object cre‐
ation. For example:
a = 1 ; // Assignment
x *= 2 ; // Assignment with operation
i ++; // Post-increment
-- c ; // Pre-decrement
System . out . println ( "statement" ); // Method invocation
Compound Statements
A compound statement is any number and kind of statements grouped together
within curly braces. You can use a compound statement anywhere a statement is
required by Java syntax:
for ( int i = 0 ; i < 10 ; i ++) {
a [ i ]++; // Body of this loop is a compound statement.
b [ i ]--; // It consists of two expression statements
} // within curly braces.
The Empty Statement
An empty statement in Java is written as a single semicolon. The empty statement
doesn't do anything, but the syntax is occasionally useful. For example, you can use
it to indicate an empty loop body in a for loop:
for ( int i = 0 ; i < 10 ; a [ i ++]++) // Increment array elements
/* empty */ ; // Loop body is empty statement
Labeled Statements
A labeled statement is simply a statement that has been given a name by prepending
an identifier and a colon to it. Labels are used by the break and continue state‐
ments. For example:
rowLoop: for ( int r = 0 ; r < rows . length ; r ++) { // Labeled loop
colLoop: for ( int c = 0 ; c < columns . length ; c ++) { // Another one
break rowLoop ; // Use a label
}
}
Local Variable Declaration Statements
A local variable , often simply called a variable, is a symbolic name for a location to
store a value that is defined within a method or compound statement. All variables
must be declared before they can be used; this is done with a variable declaration
statement. Because Java is a statically typed language, a variable declaration specifies
the type of the variable, and only values of that type can be stored in the variable.
In its simplest form, a variable declaration specifies a variable's type and name:
Search WWH ::




Custom Search