Is it a bad idea if equals(null) throws NullPointerException instead?

前端 未结 12 722
借酒劲吻你
借酒劲吻你 2020-12-02 07:00

The contract of equals with regards to null, is as follows:

For any non-null reference value x, x.equals(null) sho

12条回答
  •  隐瞒了意图╮
    2020-12-02 07:24

    Think of how .equals is related to == and .compareTo is related to the comparison operators >, <, >=, <=.

    If you're going to argue that using .equals to compare an object to null should throw a NPE, then you'd have to say that this code should throw one as well:

    Object o1 = new Object();
    Object o2 = null;
    boolean b = (o1 == o2); // should throw NPE here!
    

    The difference between o1.equals(o2) and o2.equals(o1) is that in the first case you're comparing something to null, similar to o1 == o2, while in the second case, the equals method is never actually executed so there's no comparison happening at all.

    Regarding the .compareTo contract, comparing a non-null object with a null object is like trying do this:

    int j = 0;
    if(j > null) { 
       ... 
    }
    

    Obviously this won't compile. You can use auto-unboxing to make it compile, but you get a NPE when you do the comparison, which is consistent with the .compareTo contract:

    Integer i = null;
    int j = 0;
    if(j > i) { // NPE
       ... 
    }
    

提交回复
热议问题