How to Check a Long for Null in Java

validate long variable if null

long late_in = 0;
if(latein_hours!=null){
late_in= latein_hours.getTime();
}

Primitive can't be null, only reference to object can hold null value

How to detect if a long type is actually NULL?

Assuming member.getReferral() returns a Long, use:

if (member.getReferral() != null)

In Hibernate, if you want to be able to detect nullability in a property, you must not use primitive types, because they will always have a default value 0 for longs.

Best way to check for null values in Java?

Method 4 is best.

if(foo != null && foo.bar()) {
someStuff();
}

will use short-circuit evaluation, meaning it ends if the first condition of a logical AND is false.

How do i compare a Long variable to null

You should probably be doing

String line;
while ((line = bufferedReader.readLine()) != null)
{
currentNum = Long.parseLong(line);
}

As readLine() will return null when the end of file is reached.

Nullsafe Long valueOf

You can use the NumberUtils from Apache Commons. It's null-safe and you can optionally specify a default value.

Example:

NumberUtils.toLong(null) = 0L
NumberUtils.toLong("") = 0L
NumberUtils.toLong("1") = 1L

NumberUtils.toLong(null, 1L) = 1L
NumberUtils.toLong("", 1L) = 1L
NumberUtils.toLong("1", 0L) = 1L

For more info, check the API.



Related Topics



Leave a reply



Submit