以下代码
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
}
}
使用 V1.7.0_15 编译没有错误javac
,并在运行时打印“false”。但是,Eclipse Juno 抱怨“不兼容的操作数类型 Object 和 boolean”。
显然 javac 将原始 boolean自动装箱b
,然后通过对象相等性进行比较o
和自动装箱, yielding ,而 Eclipse 拒绝进行自动装箱。b
false
根据 Java 语言规范,哪种行为是正确的?我应该在哪里提交错误?
注意:如果我将类型更改o
为Boolean
,事情会按预期工作:Eclipse 接受代码,并且代码打印“true”。