何时未分配的表达式是有效的语句? [英] When is an unassigned expression a valid statement?
问题描述
我已阅读 Oracle的表达式教程并且无法理解这一点。
I've read Oracle's expressions tutorial and couldn't understand this.
众所周知,以下代码行是有效的Java语法:
It is well known that the following line of code is valid Java syntax:
new Object();
然而,当我尝试使用原始表达式时:
However, when I try this with a primitive expression:
(3 + 2);
Eclipse显示编译错误赋值的左侧必须是变量 。
Eclipse is showing a compile error of "The left-hand side of an assignment must be a variable".
这不仅适用于原语,也适用于字符串文字:
This is true not only for primitives, but also for String literals:
"arbitraryString";
那么,未分配的表达式作为Java代码行有效的规则是什么?
So what is the rule for an unassigned expression to be valid as a Java line of code?
推荐答案
该规则位于 Java语言规范:
某些通过用分号跟随它们,可以使用各种表达式。
Certain kinds of expressions may be used as statements by following them with semicolons.
ExpressionStatement:
ExpressionStatement:
- StatementExpression;
StatementExpression:
StatementExpression:
- 作业
- PreIncrementExpression
- PreDecrementExpression
- PostIncrementExpression
- PostDecrementExpression
- MethodInvocation
- ClassInstanceCreationExpression
- Assignment
- PreIncrementExpression
- PreDecrementExpression
- PostIncrementExpression
- PostDecrementExpression
- MethodInvocation
- ClassInstanceCreationExpression
您会看到构造函数调用为tatement。但字符串文字或数学表达式不是。
You see that a constructor invocation is a statement. But a String literal or mathematical expression is not.
这篇关于何时未分配的表达式是有效的语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!