The following code
public class TestComparison {
public static void main(String[] args) throws Exception {
boolean b = true;
Object o = new Boolean(true);
System.out.println("comparison result: "+
(o == b)); // Eclipse complains about this expression
}
}
compiles without errors with javac
V1.7.0_15, and prints "false" when run. However, Eclipse Juno complains about "Incompatible operand types Object and boolean".
Apparently javac autoboxes the primitive boolean b
, and then compares o
and autoboxed b
by object equality, yielding false
, while Eclipse refuses to do the autoboxing.
Which is the right behaviour according to the Java Language Specification? Where should I file a bug?
Note: If I change the type of o
to Boolean
, things work as expected: Eclipse accepts the code, and the code prints "true".
b
and use reference equality (printing false) or unboxo
and use primitive boolean equality (printing true)? – Ritualizeo
isObject
. – Demarcation