Did you try to compile and run this program? It will not print "b is true". If you get "b is true", then you're not compiling and running exactly the program you posted above. Probably your real program has a line
*edit* Ah, I see you've edited your code...
It prints "b is true" because b = true assigns the value 'true' to b, and the result of the expression is also true.
Nirmit Dalal wrote:Why does it only happen if assignment operation of boolean variable is done in the condition expression of IF statement and not with other data type?
Assignments have a value of the right-hand-side of the expression. So, if you assign any other data type, then the value is of the any other data type. And since IF conditions only take a boolean, this would be a compile time error.
The assignment statement works like that for all types. So if i is an int, for example, then i = 2 evaluates to 2.
But the difference is that in Java the condition in an if statement has to be a boolean value. if(i = 2) doesn't type check, so the compiler will reject it. boolean variables are the only ones where = and == return the same type.