Nicely written check for numbers that can be null in java

173 Views Asked by At

I have an application where I need to handle a lot of numbers (Integers or Longs) comming from external sources.

The numbers can be null. In case they are null I always need to convert them to 0.

The problem seems trivial, but I don't want to write hundreds of times:

if (someNumber == null) {
    someNumber = 0;
} 

I don't like it for two reasons:

  1. I don't like to write three lines of code for such simple task, especially because I need to do it many times
  2. I don't like to to "mutate" someNumber (assign new value to someNumber variable)

I tried some other ways which can be seen here:

  public static void main(String[] args) {
    Integer zeroOrNull = new Random().nextBoolean() ? 0 : null;


    // version1: this is nasty (I already mentioned why)
    if (zeroOrNull == null) {
      zeroOrNull = 0;
    }

    // version2: this seems to much for so simple task...
    zeroOrNull = Optional.ofNullable(zeroOrNull).orElseGet(() -> 0);

    // version3: creating an util might be considerable. Is there already such predefined util ?
    zeroOrNull = MyUtil.getValueOrZero(zeroOrNull); // returns value or )

    System.out.println(zeroOrNull); // I want 0 here in case of null


  }

What is the preffered and nice way to do such "test for null/conversion to 0" ? Any chance to do this conversion implicitly?

5

There are 5 best solutions below

6
On

Use java8 java.util.Optional class is very efficient, but don't use explicit null values, use empty Optional

This is my original answer, if "efficient" was intended only by "performance" I've wronged word, sorry.

The mean is that all coding process is better, develop and execution are more safe.

http://www.oracle.com/technetwork/articles/java/java8-optional-2175753.html

1
On

A simple solution would be to write a utility method like below:

public static Integer checkNullNumber(Integer i){
if(i == null)
   return 0;
return i;
}

Now you can use this method wherever you want like:

zeroOrNull = checkNullNumber(zeroOrNull);
1
On

I don't like to write three lines of code for such simple task, especially because I need to do it many times

if (zeroOrNull == null) zeroOrNull = 0;

I don't like to to "mutate" someNumber (assign new value to someNumber variable)

there is no way to change the null-value into a "0" without constructing a new Integer object that holds this "0" and this single line of code does just that

0
On

Use i = (i==null)?0:i;

  • one line check
  • no method call
  • plain and simple
  • no additional dependency (unlike some other proposed solutions)

Place this check as close to your numbers source as possible, to avoid unnecessary duplications.

0
On

By creating overloaded versions of Null Checker

public static void main(String args[]) {

    Integer intObj = null;
    System.out.println("intObj : " + checkNull(intObj));

    intObj = 1122222;
    System.out.println("intObj : " + checkNull(intObj));

    Long longObj = null;
    System.out.println("longObj : " + checkNull(longObj));

    longObj = 666555556L;
    System.out.println("longObj : " + checkNull(longObj));

    System.out.println("*********With default value***********");

    intObj = null;
    System.out.println("intObj : " + checkNull(intObj, 1));

    intObj = 1122222;
    System.out.println("intObj : " + checkNull(intObj, 1));

    longObj = null;
    System.out.println("longObj : " + checkNull(longObj, 0L));

    longObj = 666555556L;
    System.out.println("longObj : " + checkNull(longObj, 0L));
}

static Integer checkNull(Integer obj) {
    if (obj == null)
        return 0;
    return obj;
}

static Long checkNull(Long obj) {
    if (obj == null)
        return 0L;
    return obj;
}

static Integer checkNull(Integer obj, int i) {
    if (obj == null)
        return i;
    return obj;
}

static Long checkNull(Long obj, long l) {
    if (obj == null)
        return l;
    return obj;
}