Zero string and integer values ​​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 get NullPointerException only in the last line?

+49
java string nullpointerexception autoboxing addition
Jan 26 '13 at 16:24
source share
6 answers

Your code uses two different additive operators . The first three lines use string concatenation , while the last uses numerical append .

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

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

Therefore, there is no NPE.

Adding two Integer objects together requires them to be unboxed . This causes the null link to break, which leads to NPE:

  • If r is null, then when unpacking the conversion, a NullPointerException
+60
Jan 26 '13 at 16:32
source share

Note that the first three operators of the + operator are related to string concatenation. Only the latter is the actual numerical sum. When string concatenation is involved (where the s variable is involved), the Java compiler uses some smart tricks to improve performance. It replaces the + operator with StringBuilder . For example, your first line is translated into:

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

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

In the last line, the situation is different. There you refer to two Integer objects. The only thing the JVM can do here is to unzip them ( i.intValue() ) and do the actual calculation. Unboxing null raises a NullPointerException .

+17
Jan 26 '13 at 16:27
source share

Concatenating (the + operator) everything with a String results in a String . Each operand is first converted to String (either using toString() , or using the value "null" ), then concatenated.

But the last operation includes only Integer , so the previous rules do not apply. Instead of concatenating, it makes an addition. But to add two Integer s, it converts objects ( Integer s) to primitive values ​​(int), which is impossible if Integer is null. That is why you get a NullPointerException .

+5
Jan 26 '13 at 16:29
source share

This seems to be a case of automatic unpacking. If you have two Integer first and second , then the result of adding them will be first.intValue() + second.intValue() . Since both of them are zero, which leads to NPE.

+1
Jan 26 '13 at 16:28
source share

Look at the bytecode for your program. You will notice that your null object is 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(); } 
0
Jan 26 '13 at 16:41
source share

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

 C:\Temp>java Test nullnull nullnull nullnull NullPointerException 
0
Feb 01 '13 at 17:51
source share



All Articles