编译错误:赋值的左侧必须是变量
class A {
public static void main(String[] args) {
for(true;true;true) {//compilation error
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试这种方式时,没有编译错误
class A {
public static void main(String[] args) {
for (getBoolean(); true; getBoolean()) {
}
}
public static boolean getBoolean() {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
getBoolean()返回一个布尔值,所以对于第一种情况,为什么for循环不直接接受布尔值?
PM *_*7-1 14
来自JLS:
BasicForStatement:
for ( ForInitopt ; Expressionopt ; ForUpdateopt ) Statement
ForStatementNoShortIf:
for ( ForInitopt ; Expressionopt ; ForUpdateopt ) StatementNoShortIf
ForInit:
StatementExpressionList
LocalVariableDeclaration
ForUpdate:
StatementExpressionList
StatementExpressionList:
StatementExpression
StatementExpressionList , StatementExpression
Run Code Online (Sandbox Code Playgroud)
然后:
StatementExpression:
Assignment
PreIncrementExpression
PreDecrementExpression
PostIncrementExpression
PostDecrementExpression
MethodInvocation
ClassInstanceCreationExpression
Run Code Online (Sandbox Code Playgroud)
正如你所看到的那样Method Invocation
是允许的而literal value
不是.
归档时间: |
|
查看次数: |
248 次 |
最近记录: |