This code will compile and run. Notice the assignment operator(=) in the condition.
boolean a;
boolean b = true;
if(a=b){
//do anything;
//this compiles
}
But this won’t compile
int a;
int b = 5;
if(a=b){
//notice its still = not ==
//do anything;
//this does not compile
}
The reason is because the previous code’s a is of
booleantype and the latter’s is of
inttype.
ifstatement is not necessarily followed by a condition.
a=bis not a condition. Its an assignment. But it works if the left variable (i.e. a) is of a boolean type.
So its better if we think
if(boolean)instead of
if(condition). Anyway, a condition like
a==b or a>b etc will result a boolean result.