Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to check null? [duplicate]

Tags:

Some people recommend method 2 for null-check, but I am not sure what's the reason behind it and why it should be preferred?

Date test1 = null; // test1 can be any object like String instead of Date
if (test1 != null) {
  System.out.println("Test");
}

Method 2:-

Date test1 = null;
if (null != test1) {
  System.out.println("Test");
}
like image 424
scott miles Avatar asked Apr 27 '17 11:04

scott miles


2 Answers

Java 7 introduced java.lang.Objects with nice little helper methods such as requireNonNull(). Java 8 added a few more, especially isNull() and nonNull(). They were meant to be used for stream() operations; but of course, they are not restricted to that scenario.

So, one alternative option would to simply rely on those new methods to make such decisions. Concise, readable, "standard; and preventing you from repeating your own check all over the place. And zero chance to introduce and kind of typos. Of course, minimal overhead from having another method call. On the hand: if your method is called millions of times - it will be JIT'ed anyway; and then such small methods might be inlined easily.

like image 60
GhostCat Avatar answered Sep 25 '22 10:09

GhostCat


There is no real difference here. Both conditions will work same way. Discussion most likely came from Yoda conditions where we write:

 null          ==         temp1
literal    comparison    variable 

which prevents us from making mistake of writing = (assignment operator) instead of == (comparison) when we could end up with code like if (foo = 42) which in some languages compiles fine causing logical errors possibly hard to find (especially by novice programmer).

But in your case you don't need to use that construct. There are at lest two reasons for that:

  1. You are using != not ==, so there is very low chance that you would write only = instead of !=

  2. In Java if (expression) expect expression to return boolean value, so even if by mistake you write = null such code will not compile since expression will return null. That prevents us from running code with such typo.

    Yoda condition in Java only makes sense for boolean expressions like if (stop == true), but even then instead of writing if (true == stop) we should simply skip == true (or ==false) part since we already have boolean value. So we should write if (stop) or if (!stop) instead.

Using such style makes sense when you are programming in different languages and you want to simplify your life by using one style which helps in one language and at the same time doesn't cause problems in other languages (even if it is not really necessary there).

Still it is worth knowing where construct/style is helpful or necessary and where it is not since each language may have better way of handling that matter like already mentioned in other answer Objects.nonNull.

like image 26
Pshemo Avatar answered Sep 23 '22 10:09

Pshemo