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:
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?
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).
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.
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.
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.
Use i = (i==null)?0:i;
Place this check as close to your numbers source as possible, to avoid unnecessary duplications.
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;
}
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);
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
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