When is an unassigned expression a valid statement?

前端 未结 4 733
一生所求
一生所求 2020-12-20 11:23

I\'ve read Oracle\'s expressions tutorial and couldn\'t understand this.

It is well known that the following line of code is valid Java syntax:

new O         


        
4条回答
  •  醉梦人生
    2020-12-20 11:56

    The rule is in the Java Language Specification:

    Certain kinds of expressions may be used as statements by following them with semicolons.

    ExpressionStatement:

    • StatementExpression ;

    StatementExpression:

    • Assignment
    • PreIncrementExpression
    • PreDecrementExpression
    • PostIncrementExpression
    • PostDecrementExpression
    • MethodInvocation
    • ClassInstanceCreationExpression

    You see that a constructor invocation is a statement. But a String literal or mathematical expression is not.

提交回复
热议问题