10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

BLOCKS AND STATEMENTS Expression Statements 14.8<br />

A labeled statement is executed by executing the immediately contained<br />

Statement. If the statement is labeled by an Identifier and the contained Statement<br />

completes abruptly because of a break with the same Identifier, then the labeled<br />

statement completes normally. In all other cases of abrupt completion of the<br />

Statement, the labeled statement completes abruptly for the same reason.<br />

14.8 Expression Statements<br />

Certain kinds of expressions may be used as statements by following them with<br />

semicolons:<br />

ExpressionStatement:<br />

StatementExpression ;<br />

StatementExpression:<br />

Assignment<br />

PreIncrementExpression<br />

PreDecrementExpression<br />

PostIncrementExpression<br />

PostDecrementExpression<br />

MethodInvocation<br />

ClassInstanceCreationExpression<br />

An expression statement is executed by evaluating the expression; if the<br />

expression has a value, the value is discarded. Execution of the expression statement<br />

completes normally if and only if evaluation of the expression completes<br />

normally.<br />

Unlike C and C++, the <strong>Java</strong> programming language allows only certain forms<br />

of expressions to be used as expression statements. Note that the <strong>Java</strong> programming<br />

language does not allow a “cast to void”—void is not a type—so the traditional<br />

C trick of writing an expression statement such as:<br />

(void) ... ;// incorrect!<br />

does not work. On the other hand, the language allows all the most useful kinds of<br />

expressions in expressions statements, and it does not require a method invocation<br />

used as an expression statement to invoke a void method, so such a trick is almost<br />

never needed. If a trick is needed, either an assignment statement (§15.26) or a<br />

local variable declaration statement (§14.4) can be used instead.<br />

DRAFT<br />

371

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!