Null values of Strings and Integers in Java
Asked Answered
G

6

49
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?

Glottis answered 26/1, 2013 at 16:24 Comment(2)
null string concatenation in java: #4261223Behave
plus 1 for such a nice observational questionGagnon
J
60

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
Jemmy answered 26/1, 2013 at 16:32 Comment(4)
+1 for quoting JLS rather then diving into implementation details (see my answer ;-)).Maurey
@TomaszNurkiewicz: I felt obliged to chime in on a question about NPE. :)Jemmy
@Jemmy Why does adding two Integer objects requires them to be unboxed ?Nudity
@Geek: Because the JLS says so: docs.oracle.com/javase/specs/jls/se7/html/…Jemmy
M
17

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.

Maurey answered 26/1, 2013 at 16:27 Comment(0)
T
5

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.

Tyro answered 26/1, 2013 at 16:29 Comment(2)
1. Every operand is first converted to a String - nope, null.toString() still throws. 2. System.out.println() accepts Object and handles null fine, no toString() is really needed.Maurey
I didn't say that toString() was applied, but that null values are converted to a String containing "null". Perhaps I was not clear enough, I agree :) Answer updated.Tyro
M
1

Seems to be a case of auto-unboxing. If you've got two Integers first and second, then the result of their addition will be first.intValue() + second.intValue(). Since both of them are null, that results in an NPE.

Masjid answered 26/1, 2013 at 16:28 Comment(0)
S
0

Have a look at the bytecode for you program. You will notice that your null object is being passed as a parameter to the PrintStream.print() method. The source code for the print() method uses String.valueOf() as shown below:

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}
Septuagenarian answered 26/1, 2013 at 16:41 Comment(0)
K
0

It should be noted that the answer to this question should have been obvious from the output:

C:\Temp>java Test
nullnull
nullnull
nullnull
NullPointerException
Karen answered 1/2, 2013 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.