How to check a Long for null in java

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.


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

There are 8 primitive types in Java:

Data Type Size Description
byte 1 byte Int8
short 2 bytes Int16
int 4 bytes Int32
long 8 bytes Int64
float 4 bytes Single
double 8 bytes Double
boolean 1 bit Boolean

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.