| Java Statements (JJ pg108) |
| Statement |
Description |
| empty |
consists of ; and performs no operation |
| block |
group of statements enclosed in {}. Treated as a single statement when used with other statements
{ x +=y;
if( x < 10 )
return y;
}
|
| declaration |
declares a variable with a particular type and optionally assigns a value: int x = 10; |
| labeled |
any statment may be labled using identifier:
startLoop:
for( ;; ){}
|
| assignment |
evaluates an expression and assigns the result to a variable:
x = y + z; |
| invocation |
calls an object method: s.toString(); |
| return |
returns a value from a method call: return x; |
| Object creation |
creates a new instance of a given class: String s = new String("abc"); |
| if..else |
selects between two alternatives
if( a==b )
// do this
else
// do this
|
| switch |
selects from various alternatives
switch( a ) {
case 1:
case 2:
case 3:
default:
|
| for |
executes a set of statements for a defined number of iterations
for( int i=0; i<10; i++ ) {
// do this
}
|
| while |
executes a block of statements while a condition is true
while( !done ) {
// do this
}
|
| do |
executes a block of statments while a condition is false
do {
// this
}while( !done );
|
| break |
transfers the flow of control to a labeled block or out of an enclosing statement |
| continue |
forces a loop to start the next iteration |
| try-catch-finally |
catches and processes exception errors that occur during the execution of a given block of code
try {
// some operation
} catch (Exception e) {
// handle the exception
} finally {
// do this
}
|
| throw |
throw an exception |
| synchronized |
gets a lock on an object and executes a statement block
synchronized(obj){
obj.setProperty(x);
}
|