difference between null != something and something != null

前端 未结 7 1667
庸人自扰
庸人自扰 2021-01-22 11:30

Is there a difference between null != something and something != null in Java. And if there is a difference then which one should I use and why??

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-22 12:20

    There's no difference between null != something and something != null. You must be thinking about the person.getName().equals("john") and the "john".equals(person.getName()) difference: the first one will throw a NullPointerException if getName() returns null, while the second won't. But this is not applicable for the example of your question.

提交回复
热议问题