| CHAPTER 14: Blocks and Statements |
Previous |
Java Language |
Index |
Next |
Certain kinds of expressions may be used as statements by following them with semicolons:
ExpressionStatement: StatementExpression ; StatementExpression: Assignment PreIncrementExpression PreDecrementExpression PostIncrementExpression PostDecrementExpression MethodInvocation ClassInstanceCreationExpression
An expression statement is executed by evaluating the expression; if the expression has a value, the value is discarded. Execution of the expression statement completes normally if and only if evaluation of the expression completes normally.
Unlike C and C++, the Java language allows only certain forms of expressions to be used as expression statements. Note that Java does not allow a "cast to void "-void is not a type in Java-so the traditional C trick of writing an expression statement such as:
(void) ... ; // This idiom belongs to C, not to Java!
does not work in Java. On the other hand, Java allows all the most useful kinds of expressions in expressions statements, and Java does not require a method invocation used as an expression statement to invoke a void
method, so such a trick is almost never needed. If a trick is needed, either an assignment statement (S15.25) or a local variable declaration statement (S14.3) can be used instead.
| © 1996 Sun Microsystems, Inc. All rights reserved. |