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