Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nicely written check for numbers that can be null in java

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?

like image 763
walkeros Avatar asked Jun 19 '15 12:06

walkeros


People also ask

How to check whether an integer is null or 0 in Java?

How to check whether an Integer is null or zero in Java? - Stack Overflow How to check whether an Integer is null or zero in Java? if (myInteger != null && myInteger != 0) { ... } For example, for Strings you can use StringUtils.isBlank () @RafałLaskowski that depends on the type, i.e. int vs. Integer (the latter can be null).

How do I check if a string is empty in Java?

Checking for Null or Empty in Java. Checking for Null or Empty in Java. We can check whether a particular String is empty or not, using isBlank () method of the StringUtils class. This method accepts an integer as a parameter and returns true if the given string is empty, or false if it is not.

How to avoid NullPointerException in Java?

A common way of avoiding the NullPointerException is to check for null: In the real world, programmers find it hard to identify which objects can be null. An aggressively safe strategy could be to check null for every object. However, this causes a lot of redundant null checks and makes our code less readable.

Why can't I write NULL in Java?

1. null is Case sensitive: null is literal in Java and because keywords are case-sensitive in java, we can’t write NULL or 0 as in C language. 5: error: cannot find symbol can't find symbol 'NULL' ^ variable NULL class Test 1 error 2. Reference Variable value: Any reference variable in Java has a default value null . 3.


4 Answers

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.

like image 160
plastique Avatar answered Sep 30 '22 13:09

plastique


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;
}
like image 32
Rajesh Avatar answered Sep 30 '22 12:09

Rajesh


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);
like image 20
Amol Binwade Avatar answered Sep 30 '22 13:09

Amol Binwade


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

like image 28
Nico Weisenauer Avatar answered Sep 30 '22 13:09

Nico Weisenauer