Jan Osykowski wrote:Hey guys,

I have a question about situation like this:

I thought that here the == comparison will return false because == operator looks at the references and b3 is a primitive and b2 is an Object. But for some reason, the b2 got unboxed and the comparison resulted in the true value. Can anyone tell me if this can happen with some other types in Java? Or about some similar situations that may occur?

Cheers,

Jan.

The JLS on boolean equality operator:

If the operands of an equality operator are both of type boolean, or if one operand is of type boolean and the other is of type Boolean, then the operation is boolean equality. The boolean equality operators are associative.

If one of the operands is of type Boolean it is subjected to unboxing conversion (§5.1.8).

The result of == is true if the operands (after any required unboxing conversion) are both true or both false; otherwise, the result is false.

The result of != is false if the operands are both true or both false; otherwise, the result is true. Thus != behaves the same as ^ (§15.22.2) when applied to boolean operands.

So if you compare two Boolean objects, it will compare object identity, otherwise, it will compare boolean equality.