This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenating null strings in Java
I'm just wondering if someone can explain why does the following code works the way it does:
String a = null;
String b = null;
String c = a + b;
System.out.println(c.toString());
This prints "nullnull" to the console.
I was kind of expecting that the operation + would thrown an exception or, to a lesser degree, that "c" would be null.
because that code compiles to
String c = new StringBuilder().append(a).append(b).toString();
and StringBuilder.append(a) appends the result of String.valueof(a) which is "null" if a is null
"String conversion," required by the concatenation operator, mandates that null values are to be mapped to the four characters null whenever a string representation is needed.
Spec for string conversion: http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.11
Spec for string concatenation: http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.18.1
When java find a null reference during the string concatenation convert it to the string "null". So "nullnull" is the right result.
Before concatenation for every object Java call
java.lang.String.valueOf(Object)
and if you look at the source code of that method you find this
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
Related
This question already has answers here:
What is null in Java?
(14 answers)
Closed 3 years ago.
I have a class with the two string variable i.e:
String str = null;
String str1="null";
One is a "string value null" and other in other case null object is assigned to a string variable.
So what is the difference in both the two assignments and how to check if one is different from other ??
String str = null; means str is a reference of String which points to null.
And String str1="null"; means str1 object which points to String Object which is "null".
First check if object is null or not ? then compare by using equals method.
like
if(str==null && str1==null){
//Both are null and equal
}
if(str != null && str.equals(str1)){
//return true;
}
else
{
//return false;
}
The String "null" is a string of length 4 with the characters n, u, l and l. It can be worked on as any other string.
The null reference isn't a string. Any attempt to use it like a string will result in a NullPointerException.
1) String str = null;
Here str has no instance is created because it is assigned to null, so no new memory is consumed in heap
2) String str1="null";
Here str1 string instance is created and "null" is stored as value
This question already has answers here:
Why does String.valueOf(null) throw a NullPointerException?
(4 answers)
Closed 5 years ago.
Today I found a bug in legacy code, which led me to a shocking discovery:
String s = null + ""; //results in "null" string
s = String.valueOf(null); // NullPointerException is thrown
Integer i = null;
s = String.valueOf(i); //results in "null" string
First question is: Why such an unexpected behavior happens?
It mean that a convenient code like this:
String age = person.getAge() + "";
is being totally unexpected.
Second question: What is the best (most elegant) way to get a null instead of "null"
String.valueOf(null) calls public static String valueOf(char data[]) due to method overloading resolution rules (char[] is more specific than Object). That method throws NullPointException for a null argument.
String.valueOf(i) calls public static String valueOf(Object obj), which returns the String "null" for a null argument.
To avoid this exception, either don't call s = String.valueOf(null); (you can always assign s = "null"; directly, or cast the null value to some type other than char[] (for example s = String.valueOf((String)null);).
Converting the value to String in java; There are multiple ways of doing it.
Just wanted to know what's the difference between each other in the following ways.
strValue.toString()
strValue+""
""+strValue
It depends on java version. Java 7 would act a bit smarter using StringBuilder + append().
Generally, you do not want unnecessary allocations. Use first one.
strValue.toString()
will return itself, because the toString() implementation of String (I'm guessing strValue is indeed of type String) returns this.
strValue+""
""+strValue
Will result in the same value (strValue) but won't invoke the toString() method
All Strings contain the same value, try it out:
String strValue = "Hello world"; // not null
String a = strValue.toString();
String b = strValue+"";
String c = ""+strValue;
Measuring its length give all the result 11, because adding an empty String to another one equals the original String itself:
System.out.println(a.length());
...
Try the equality between these Strings:
System.out.println(a.equals(b));
System.out.println(b.equals(c));
System.out.println(c.equals(a));
They are all true, because these Strings have the same value to be compared. All it in the case the strValue is not null.
One major difference is how null is handled.
If strValue is null, strValue.toString() will throw a NullPointerException, while the other two options will return the String "null".
Other differences may be observed if strValue is of a boxed numeric type, and you try to concatenate other numeric variables to it.
For example :
If
Integer a = 5;
Integer strValue = 6;
Then
a+strValue+""
would return
"11"
while
a+""+strValue
or
""+a+strValue
would return
"56"
This is a follow-up question to some previous questions about String initialization in Java.
After some small tests in Java, I'm facing the following question:
Why can I execute this statement
String concatenated = str2 + " a_literal_string";
when str2 a String object initialized to null (String str2 = null;) but I cannot call the method toString() on str2? Then how does Java do the concatenation of a null String object and a string literal?
By the way, I tried also to concatenate an Integer initialized to null and the string literal "a_literal_string" and I've got the same thing that is "null a_literal_string" in the console. So whichever kind of null gives the same thing?
PS : System.out.println(concatenated); gives null a_literal_string as output in the console.
This line:
String concatenated = str2 + " a_literal_string";
is compiled into something like
String concatenated = new StringBuilder().append(str2)
.append(" a_literal_string")
.toString();
This gives "null a_literal_string" (and not NullPointerException) because StringBuilder.append is implemented using String.valueOf, and String.valueOf(null) returns the string "null".
I tried also to concatenate an Integer initialized to null and the string literal "a_literal_string" and I've got the same thing
This is for the same reason as above. String.valueOf(anyObject) where anyObject is null will give back "null".
This question already has answers here:
String concatenation with Null
(3 answers)
Closed 3 years ago.
Why does the following work? I would expect a NullPointerException to be thrown.
String s = null;
s = s + "hello";
System.out.println(s); // prints "nullhello"
Why must it work?
The JLS 5, Section 15.18.1.1 JLS 8 § 15.18.1 "String Concatenation Operator +", leading to JLS 8, § 5.1.11 "String Conversion", requires this operation to succeed without failure:
...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.
How does it work?
Let's look at the bytecode! The compiler takes your code:
String s = null;
s = s + "hello";
System.out.println(s); // prints "nullhello"
and compiles it into bytecode as if you had instead written this:
String s = null;
s = new StringBuilder(String.valueOf(s)).append("hello").toString();
System.out.println(s); // prints "nullhello"
(You can do so yourself by using javap -c)
The append methods of StringBuilder all handle null just fine. In this case because null is the first argument, String.valueOf() is invoked instead since StringBuilder does not have a constructor that takes any arbitrary reference type.
If you were to have done s = "hello" + s instead, the equivalent code would be:
s = new StringBuilder("hello").append(s).toString();
where in this case the append method takes the null and then delegates it to String.valueOf().
Note: String concatenation is actually one of the rare places where the compiler gets to decide which optimization(s) to perform. As such, the "exact equivalent" code may differ from compiler to compiler. This optimization is allowed by JLS, Section 15.18.1.2:
To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.
The compiler I used to determine the "equivalent code" above was Eclipse's compiler, ecj.
See section 5.4 and 15.18 of the Java Language specification:
String conversion applies only to the
operands of the binary + operator when
one of the arguments is a String. In
this single special case, the other
argument to the + is converted to a
String, and a new String which is the
concatenation of the two strings is
the result of the +. String conversion
is specified in detail within the
description of the string
concatenation + operator.
and
If only one operand expression is of
type String, then string conversion is
performed on the other operand to
produce a string at run time. The
result is a reference to a String
object (newly created, unless the
expression is a compile-time constant
expression (§15.28))that is the
concatenation of the two operand
strings. The characters of the
left-hand operand precede the
characters of the right-hand operand
in the newly created string. If an
operand of type String is null, then
the string "null" is used instead of
that operand.
The second line is transformed to the following code:
s = (new StringBuilder()).append((String)null).append("hello").toString();
The append methods can handle null arguments.
You are not using the "null" and therefore you don't get the exception. If you want the NullPointer, just do
String s = null;
s = s.toString() + "hello";
And I think what you want to do is:
String s = "";
s = s + "hello";
This is behavior specified in the Java API's String.valueOf(Object) method. When you do concatenation, valueOf is used to get the String representation. There is a special case if the Object is null, in which case the string "null" is used.
public static String valueOf(Object obj)
Returns the string representation of the Object argument.
Parameters:
obj - an Object.
Returns:
if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.