Search code examples
javanulllong-integer

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; }

Solution

  • 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)