Java – allows equal comparison between ‘Boolean’ and ‘object’?
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 } }
Using javac v1.0 7.0_ Compile without errors and print "false" at run time However, eclipse Juno complains about "incompatible operand types object and Boolean"
Obviously, javac auto@R_512_2419 @The original Boolean B of ES, and then compare O and auto@R_512_2419 @The objects of ED and B are equal, resulting in false, while eclipse refuses to perform automatic boxing
According to the Java language specification, which is the correct behavior? Where should I make a mistake?
Note: if I change the type of O to Boolean, things work as expected: Eclipse accepts the code and the code prints "true"
Runnable version on ideone. com
Solution
This is your project language level setting You may be using a Java 7 compiler with Java 6 semantics I don't have eclipse here, but I copied it in IntelliJ. When the language level is on Java 6, even if the compiler I use is 7 I guess so is eclipse This link explains