Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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; } 
like image 605
BrownTownCoder Avatar asked Oct 29 '14 18:10

BrownTownCoder


People also ask

Can we compare long with null in Java?

longValue() method or using casting, we should check if the object is null. We could have a NullPointerException if the object is null.

How do you test for null in Java?

In order to check whether a Java object is Null or not, we can either use the isNull() method of the Objects class or comparison operator.

Does long support null?

Whereas a Long object reference can be null, a primitive long can not.


2 Answers

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) 
like image 98
brso05 Avatar answered Sep 19 '22 09:09

brso05


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.

like image 33
Diablo Avatar answered Sep 19 '22 09:09

Diablo