Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why String concatenate null with + operator and throws NullPointerException with concate() method

Tags:

java

string

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 ??

like image 428
kavi temre Avatar asked May 02 '15 14:05

kavi temre


People also ask

What is wrong with string concatenation in loop?

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.

What happens if you add null to a string?

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.

Can we concatenate string with null?

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.

What are the 2 methods used for string concatenation?

There are two ways to concatenate strings in Java: By + (String concatenation) operator. By concat() method.


4 Answers

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.

like image 78
singhakash Avatar answered Oct 11 '22 16:10

singhakash


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);
}
like image 42
Zigac Avatar answered Oct 11 '22 17:10

Zigac


@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.

like image 4
Jack Avatar answered Oct 11 '22 15:10

Jack


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);
    }
like image 2
sol4me Avatar answered Oct 11 '22 16:10

sol4me