If statement is not always followed by a condition in java

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

boolean
type and the latter’s is of
int
type.

if
statement is not necessarily followed by a condition.
a=b
is 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.