why "abc" + null results abcnull
why "abc" + null
results 开发者_如何学Pythonabcnull
String s1 = "abc";
String s2 = null;
String s3 = s1+ s2;
System.out.println(s3);
Result: abcnull
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 String
s, 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.
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.)
That's how string concatenation works.
If you need to avoid concatenating the word "null" you can do:
String s3 = (s1==null?"":s1) + (s2==null?"":s2);
i don't know java, but from the looks of it, it seems like you are type casting s2
as a String object which then turns null
into "null"
. Please correct me if i'm wrong.
String s = "a" + null;
compiler understands it as
String s = "a" + String.valueOf(null)
so
String s = "a" + String.valueOf(null)/* null*/ = "anull";
In your example
String s3 = String.valueOf(s1) + String.valueOf(s2);
s3 = "abc" + "null" // absnull
String Concatenation in Java executes automatic conversion. The null string will be converted to the string 'null'. For reference, see f.e. this page:
A null operand is converted to the string literal "null".
To avoid this, you should check if one string is null yourself.
This is how Java Concatenation works. It treats null objects as "null".
Please see this link
it is because any thing u add it with a string will by default convert it into a string. and even if you try to add a number to a string it will be converted into string. example: "abc"+123="abc123" there is a syntax called concat which will add only string to string not any thing else. example : if u type "abc".concat(null) then it will show java.lang.NullPointerException at java.lang.String.concat(Unknown Source) but if u type "abc".concat("null") the output will be "abcnull".
Let's compile your code and look at the bytecode:
// -- Initialization of s1
5: ldc #2; //String abc
7: putfield #3; //Field s1
// ...
// -- Initialization of s2
11: aconst_null
12: putfield #4; //Field s2
// ...
// -- Initialization of s3
// create a StringBuilder
16: new #5; //class StringBuilder
19: dup
20: invokespecial #6; //Method StringBuilder."<init>"
// Load s1
23: aload_0
24: getfield #3; //Field s1
// Call StringBuilder.append(String)
27: invokevirtual #7; //Method StringBuilder.append(String)
30: aload_0
// Load s2
31: getfield #4; //Field s2
// Call StringBuilder.append(String)
34: invokevirtual #7; //Method StringBuilder.append(String)
// Get content of StringBuilder
37: invokevirtual #8; //Method StringBuilder.toString()
// Store in s3
40: putfield #9; //Field s3
So how does "null"
end up in the result? Well, this is described in the documentation of StringBuilder.append(String)
:
The characters of the String argument are appended, in order, increasing the length of this sequence by the length of the argument. If str is null, then the four characters "null" are appended.
As Jon Skeet points out, this is all in accordance with the Java Language Specification.
精彩评论