difference between null != something and something != null

前端 未结 7 1754
庸人自扰
庸人自扰 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:17

    Point of view of performance there will be no difference, both sides of the operator are executed any way. But for a more readable code second one seems more readable

      obj.getSomething().getAnotherThing().doSomething() != null
    
      null != obj.getSomething().getAnotherThing().doSomething()
    

    But if you are going to just compare a variable or parameter this is more readable

      something != null
    

    Of course this depends on sense of reader.

提交回复
热议问题