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

前端 未结 12 706
借酒劲吻你
借酒劲吻你 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:33

    Personally, I'd rather it perform as it does.

    The NullPointerException identifies that the problem is in the object against which the equals operation is being performed.

    If the NullPointerException was used as you suggest and you tried the (sort of pointless) operation of...

    o1.equals(o1) where o1= null... Is the NullPointerException thrown because your comparison function is screwed or because o1 is null but you didn't realise? An extreme example, I know, but with current behaviour I feel you can tell easily where the problem lies.

提交回复
热议问题