Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between null != something and something != null

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??

like image 427
M.J. Avatar asked May 16 '26 01:05

M.J.


2 Answers

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.

like image 179
sp00m Avatar answered May 18 '26 13:05

sp00m


its probably comming from the so-called joda-conditions where you write "bla" == myVariable instead of myVariable == "bla" because it could happen to accidentially write myVariable = "bla" which returns "bla" in some languages but also assign "bla" to myVariable

like image 30
wrm Avatar answered May 18 '26 13:05

wrm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!