Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Null values of Strings and Integers in Java

public class Test {
    public static void main(String[] args) {

        String s = null;
        String s1 = null;
        Integer i = null;
        Integer i1 = null;

        System.out.println(s+i);
        System.out.println(i+s);
        System.out.println(s+s1);

        try {
            System.out.println(i+i1);
        } catch (NullPointerException np) {         
            System.out.print("NullPointerException");       
        }
    }

}

The question is simple - why do I receive a NullPointerException only at the last line?

like image 886
tania Avatar asked Jan 26 '13 16:01

tania


People also ask

What is null value for integer in Java?

Java Integer, Boolean and Long variables with null values come through as 0 and false when passing ProDataSets or Temp-Tables to the AppServer with the Java OpenClient.

Can string have null values in Java?

“A null String is Java is literally equal to a reserved word “null”. It means the String that does not point to any physical address.” In Java programming language, a “null” String is used to refer to nothing. It also indicates that the String variable is not actually tied to any memory location.

Is null string or integer?

null is not a valid representation of integer number. Integer. parseInt() requires that the string be parsed is a vaild representation of integer number. Integer.

What is string value of null Java?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.


3 Answers

Your code makes use of two different additive operators. The first three lines use string concatenation, whereas the last one uses numeric addition.

String concatenation is well-defined to turn null into "null":

  • If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l).

Hence there is no NPE.

Adding two Integer objects together requires them to be unboxed. This results in the null reference being dereferenced, which leads to the NPE:

  • If r is null, unboxing conversion throws a NullPointerException
like image 140
NPE Avatar answered Oct 19 '22 18:10

NPE


Notice that first three + operator usages involve string concatenation. Only the last one is the actual numeric sum. When string concatenation is involved (where s variable is involved), Java compiler uses some clever trick to improve performance. It replaces + operator with StringBuilder. For example your first line is translated to:

StringBuilder tmp = new StringBuilder();
tmp.append(s);
tmp.append(i);
System.out.println(tmp);

StringBuilder is null-friendly so no matter what you pass as an argument, it nicely replaces it with "null" string.

The situation is different in the last line. There you reference two Integer objects. The only that the JVM can do here is to unbox them (i.intValue()) and do the actual computation. Unboxing of null causes NullPointerException.

like image 31
Tomasz Nurkiewicz Avatar answered Oct 19 '22 17:10

Tomasz Nurkiewicz


The concatenation (+ operator) of anything with a String results in a String. Every operand is first converted to a String (either using toString(), or using the value "null"), then concatenated.

But the last operation involves Integers only, so the previous rules don't apply. Instead of a concatenation, it does an addition. But to add two Integers, it converts objects (Integers) to primitive values (int), which is not possible if the Integer is null. That's why you get a NullPointerException.

like image 5
Bastien Jansen Avatar answered Oct 19 '22 18:10

Bastien Jansen