Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why "abc" + null results abcnull

Tags:

java

null

why "abc" + null results abcnull

String s1 = "abc";
String s2 = null; 
String s3 = s1+ s2;

System.out.println(s3);

Result: abcnull

like image 433
banjara Avatar asked Dec 19 '25 17:12

banjara


2 Answers

Because Java will create a StringBuilder to append s1, or "abc", to s2, or null.

According to the spec for StringBuilder.append(String)--

If str is null, then the four characters "null" are appended

So it turns into the same as "abc" + "null"

Let's take your code example (I placed it inside a method):

 public static void main(String[] args)
 {
  String s1 = "abc";
  String s2 = null; 
  String s3 = s1+ s2;
  System.out.println(s3);
 }

If we take a gander at the bytecode (gotten by invoking javap -c):

public static void main(java.lang.String[]);
  Code:
   0:   ldc #2; //String abc
   2:   astore_1
   3:   aconst_null
   4:   astore_2
   5:   new #3; //class java/lang/StringBuilder
   8:   dup
   9:   invokespecial   #4; //Method java/lang/StringBuilder."<init>":()V
   12:  aload_1
   13:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   16:  aload_2
   17:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   20:  invokevirtual   #6; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   23:  astore_3
   24:  getstatic   #7; //Field java/lang/System.out:Ljava/io/PrintStream;
   27:  aload_3
   28:  invokevirtual   #8; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
   31:  return

Java creates a StringBuilder and appends the values, as Strings, of s1 and s2.

So you have to be careful then, if you were expecting a NullPointerException, because concatenation in Java will skirt the issue.

Side Note: As pointed out by Jon Skeet, this is ultimately just implementation details -- it's the Java spec that guarantees that null, when converted to a String, turns into "null". However, this bytecode at least shows what is actually happening behind the scenes.

like image 79
Zach L Avatar answered Dec 21 '25 06:12

Zach L


That's exactly the value that's guaranteed by the language specification. From the JLS section 15.8.1.1, which talks about the string conversions used for string concatenation:

Now only reference values need to be considered. If the reference is null, it is converted to the string "null" (four ASCII characters n, u, l, l). Otherwise, the conversion is performed as if by an invocation of the toString method of the referenced object with no arguments; but if the result of invoking the toString method is null, then the string "null" is used instead.

(You may be interested to hear that .NET takes a different approach, converting a null reference to an empty string. Each approach has its pros and cons - I find the Java approach more useful for creating a string for debugging, but the .NET approach more useful when building a string to be displayed to a user or saved to a data file etc.)

like image 28
Jon Skeet Avatar answered Dec 21 '25 07:12

Jon Skeet