Purpose of Objects.isNull (...) / Objects.nonNull (...)

What is the purpose

Objects.isNull(x) 

      

if we can just write

x == null

      

?

The same for

Objects.nonNull(...)

      

and

x != null

      

+30


source to share


2 answers


From JavaDoc method :



API Note: This method exists for use as Predicate

,filter(Objects::isNull)

+53


source


Apart from its obvious use in the functional world. It can also be used in your normal code instead of ==

.

Many programmers (myself included) believe that they are ( x == null

or x != null

) not object oriented
, and therefore it makes sense to use the object oriented version.

Java doc objects:



This class consists of static utility methods for working with objects. These utilities include null and invalid methods for calculating the hash code of an object, returning a string for the object, and comparing two objects.

I personally prefer the method version and have been using it for the last couple of years :)

0


source







All Articles