在最后一个条件下,我期望从运算符“&&”左侧的表达式开始评估条件.这变为真实,然后在右边的声明
.但是,我在这里收到编译错误,说“令牌上的语法错误”=“,!= 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");
编辑:谢谢你的回答.为了检查它是否是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;
}
解决方法:
编译器正在尝试评估最后一个,如果它被写入
if( (ten==10 && assignBool)=boolTrue )
这是没有意义的,因为左侧不是左值(从C借用一些词汇).这是因为&&优先级高于Java中的=.
标签:java,compiler-errors,condition
来源: https://codeday.me/bug/20190714/1455360.html