Jim*_*mmy 3 java compiler-errors conditional-statements
在最后一个条件下,我期望从运算符"&&"左侧的表达式开始计算条件,该表达式变为true,然后是右侧的语句.但是,我在这里收到编译错误,说"令牌上的语法错误"=",!= expected".对这个问题的任何解释都会有很大的帮助.
boolean boolTrue=Boolean.TRUE;
boolean assignBool=Boolean.FALSE;
int ten=10;
//eventually evaluates to true , runs okay
if(assignBool=boolTrue){
System.out.println("Executed");
}
//evaluates to true && true: runs correctly
if( assignBool=boolTrue && ten==10 )
System.out.println("Executed");
//evaluates to true && true : runs correctly
if( ten==10 && (assignBool=boolTrue) )
System.out.println("Executed");
/*was supposed to evaluate to true && true : but gives compile error
compiler expects me to use '!=' or '==' operator though the second statement ultimately evaluates to true
as in above case*/
if( ten==10 && assignBool=boolTrue )//Compile error
System.out.println("Executed");
Run Code Online (Sandbox Code Playgroud)
编辑:谢谢你的回答.为了检查它是否是operatar优先级问题,我在for循环和YES中运行相同的情况,就是这样.
boolean boolTrue=Boolean.TRUE;
boolean assignBool=Boolean.TRUE;
int ten=10;
singleloop:
for(int i=0;((ten==10 && assignBool)==boolTrue);System.out.println("Executed")){
i++;
if(i>10)
break singleloop;
}
Run Code Online (Sandbox Code Playgroud)
编译器正在尝试评估最后一个if,就像它被写入一样
if( (ten==10 && assignBool)=boolTrue )
Run Code Online (Sandbox Code Playgroud)
这是没有意义的,因为左侧不是左值(从C借用一些词汇).这是因为它&&具有比=Java 更高的优先级.