How to check a Long for null in java

How do I check a Long value for null in Java?

Will this work?

if ( longValue == null) { return blah; }

Primitive data types cannot be null. Only Object data types can be null.

int, long, etc... can't be null.

If you use Long (wrapper class for long) then you can check for null's:

Long longValue = null;

if(longValue == null)

If it is Long object then You can use longValue == null or you can use Objects.isNull(longValue) method in Java 7+ projects .

Please check Objects for more info.


If the longValue variable is of type Long (the wrapper class, not the primitive long), then yes you can check for null values.

A primitive variable needs to be initialized to some value explicitly (e.g. to 0) so its value will never be null.