Equality comparison of `boolean` and `Object` allowed?

前端 未结 2 1713
花落未央
花落未央 2021-01-04 17:47

The following code

public class TestComparison {
    public static void main(String[] args) throws Exception {
        boolean b = true;
        Object o = n         


        
相关标签:
2条回答
  • 2021-01-04 18:19

    It's your project language level setting. You are probably using a Java 7 compiler with Java 6 semantics. I don't have Eclipse here, but I reproduced it in IntelliJ, which gave errors when the language level was on Java 6, even though the compiler I used was 7. I guess Eclipse has the same. This link explains it.

    0 讨论(0)
  • 2021-01-04 18:21

    Concerning your 'Note' that the code compiles and works when o is changed to Boolean:

    This code:

    public class Tester{
      public static void main(String args[]){
        Boolean one = new Boolean(true);
        Object two = new Boolean(true);
        boolean three = true;
        System.out.println("SAME 1:2 " + (one == two) + " 1:3 " + (one == three) + " 2:3 " + (two == three));
        System.out.println("EQUAL 1:2 " + (one.equals(two)) + " 1:3 " + (one.equals(three)) + " 2:3 " + (two.equals(three)));
      }
    }
    

    produces this result:

    SAME 1:2 false 1:3 true 2:3 false
    EQUAL 1:2 true 1:3 true 2:3 true
    

    To see why this is, we need to consider the compile-time types of the various expressions:

    • one == two compares a Boolean with an Object - these are both reference types, so the test is reference equality (Java Language Specification, Java SE 7 edition, §15.21.3)
    • one == three compares a Boolean with a boolean - this is treated as a comparison of primitive boolean values (§15.21.2); one is unboxed and compared with three.
    • two == three compares an Object with a boolean - in this case the boolean is converted to Object by a casting conversion (§5.5, in this case boxing boolean to Boolean and then widening Boolean to Object) and the two are then compared for reference equality.

    The EQUAL line is much simpler - all three cases are calls to Boolean.equals(Object other), using boxing when the argument is three.

    0 讨论(0)
提交回复
热议问题