Here is my class, where i am concatenating two string.
String concatenate with null
using + operator execute smoothly but throws NullPointerException
with concate()
method.
public class Test {
public static void main(String[] args) {
String str="abc";
String strNull=null;
System.out.println(strNull+str);
str.concat(strNull);
}
}
can anybody tell me the reason behind it ??
If you concatenate Stings in loops for each iteration a new intermediate object is created in the String constant pool. This is not recommended as it causes memory issues.
If the input object is null, it returns an empty (“”) String, otherwise, it returns the same String: return value == null ? "" : value; However, as we know, String objects are immutable in Java.
To concatenate null to a string, use the + operator. Let's say the following is our string. String str = "Demo Text"; We will now see how to effortlessly concatenate null to string.
There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.
Case 1:
System.out.println(strNull+str); // will not give you exception
From the docs(String conversion)
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.
Case 2:
str.concat(strNull); //NullPointer exception
If you see the source of concat(String str)
it uses str.length();
so it would be like null.length()
giving you a NullPointerException
.
If you see in java.lang.String source, and use null as parameter. NPE is thrown in first line in length() method.
public String concat(String str) {
int otherLen = str.length();//This is where NullPointerException is thrown
if (otherLen == 0) {
return this;
}
getChars(0, count, buf, 0);
str.getChars(0, otherLen, buf, count);
return new String(0, count + otherLen, buf);
}
@Blip : 1) null + "ABC"
Straight forward answer to your question is + operator in java uses StringBuilder.append method to concat the strings.
And StringBuilder.append() method treats null object as "null" String. Hence null + "ABC" won't give Null Pointer exception but rather prints nullABC.
2) String.concat()
And yes, there is no null check before string.length() method in concat() function of String class. Hence it is throwing null pointer exception.
Hope this answers your question.
Because inside concat
method in String class, length
method is getting invoked on passed parameter , due to which NPE is thrown.
public String concat(String str) {
int otherLen = str.length();<------
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With