How do I check a Long value for null in Java?
Will this work?
if ( longValue == null) { return blah; }
longValue() method or using casting, we should check if the object is null. We could have a NullPointerException if the object is null.
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.
Whereas a Long object reference can be null, a primitive long can not.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With