At the very last condition, I was expecting to get the condition evaluated starting from the expression on the left of operator “&&” which becomes true and then the statement on the right
. However, I am getting compilation error here saying “syntax error on token “=”, != expected”.
Any explanation on this problem would be of great help.
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");
EDIT: Thanks for the answer. To check if it was operatar precedence issue, I ran same case in for loop and YES, that was it .
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;
}
The compiler is trying to evaluate the last
ifas if it were writtenwhich makes no sense because the left side is not an lvalue (to borrow some vocabulary from C). This is because
&&has higher precedence than=in Java.