Difference between concat() and + (String concatenation operator)


Both concat() and + are used to concatenate the strings but they have some differences which are described below:

1. Java.lang.NullPointerException :

concat() method if called on null String reference variable, will throw NullPointerException while this is not the case with + operator.

For example,

String s=null;
s.concat(“abc”);

will throw NullPointerException.

While

String s=null;

System.out.println(s+”abc”);

Will print nullabc. While concatenating strings with + operator, a null reference variable changes to “null” (i..e a String containing null as its contents.). That’s why s becomes “null” and output is nullabc.

2. Number of arguments:

concat() takes only one argument while + can take any number of arguments.

Example,

“I”+“ am”+” a”+” good”+” boy”

3. Type of Argument:

Signature of concat() method is:  public String concat(String str)

concat() only takes String type as argument. If any other type argument is passed to concat() it will give compile time error. Example,

s.concat(5);  // error as 5 is a non string value

s.concat(“5”);  // no error as here 5 is string (enclosed in “”)

While + can take any type of arguments. While doing concatenation non-string type argument is converted to String by using its toString() method

For example,

String s=”abc”;
int i=5;

System.out.println(s+i);  // no error, will print abc5.

Here int value 5 is a primitive value. It will be first converted to a string value as:


      int------>Integer.toString(5)-------> a String representation of int value 5.


Note that as explained in article String concatenation operator +, at least one argument of + must be of string type otherwise + will behave like mathematical addition operator instead of string concatenation operator.

4. Creation of new String Object:

You may have read that in Java Strings are immutable. Whenever you try to change its contents, a new String object with changed contents is created instead of modifying the original String object.

But concat() returns new String object only when the length of argument string is > 0.

String s=”Blue”;
String s1=”Sky!”
String s2=s.concat(s1);

Here concat() returns a new String object whose reference is stored in s2.

But if the length of argument string is 0, then concat() returns same original string instead of returning a new String object. Example,

String s=”abc”;
String s1=s.concat(“”);

Here “” (an empty String with length 0) is a passed as an argument to concat(). So no new String object created but reference to original string s is returned and strored in s1.

Therefore, s==s1  will give true here i.e both s and s1 are pointing to same objects.

While,

+ creates a new String object every time it concatenates something, except when the concatenation is done at compile time.

For example,

String s=”Hello”;

String s1=s+”World!”;

Here a new String object s1 is created with concatenated result (Hello World!).

Similarly,

String s=”Hello”;

String s1=”World!”;

String s2=s+s1;

Will create a new String object s2.

In both above example concatenation happen at run time.

But,

String s=”Hello”+”World!”;

Will not create a not create a new String object because here concatenation will happen at compile time.

There will be only one String object with contents “Hello World!”. Both String literals “Hello” and “World!” will be discarded.

5. Performance

Performance wise concat() is consider better than + operator. One of the reasons for this is that concat() returns a new String object only when length of its arguments is > 0 as stated in point 4.



I would like to know your comments and if you liked the article then please share it on social networking buttons.


8 comments:

  1. String a ="ja";
    String b="va";
    String c="do";
    String d="main";

    System.out.println(a.concat(b).concat(c).concat(d));

    So we can concat any number of strings, kindly change your second point. Thanks.

    ReplyDelete
    Replies
    1. Your point is correct in its own sense and obviously we can concat more than one string using 'concat()' repeatedly as shown in your example. But my point is saying that concat() method takes only one argument like a.concat(b), not more than one as concat(a,b) while this is not the case with '+' operator. :)

      Delete
    2. String a ="ja";
      String b="va";
      String c="do";
      String d="main";

      System.out.println(a.concat(b).concat(c).concat(d));

      In this example concat called again and again , so how many objects it will create?
      my question is a.concat(b) is one object , then concat(c) 2nd object then concat(d) will create 3rd object in stringpool. My doubt created because concat method returns string,so is it creating new new object each time it called. (I know if there is no object reference present , then it will be garbage collected)

      Delete
  2. Hey I just checked it out and this:

    String first = "first string" ;
    String second = "second string";
    String fourth = first.concat(second+"nothing");

    WORKS!

    This means that concat can indeed take more than one value as shown in line 3.
    This is the output that Eclipse returns:

    first stringsecond stringnothing

    ReplyDelete
    Replies
    1. Abhishek second+"nothing" is treated as one method argument not as two different method arguments.

      Delete