Difference between concat() and + operator in Java (original) (raw)

Last Updated : 27 Sep, 2022

Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created. Concatenation is the process of joining end-to-end.

concat() Method

The Java String concat() method of String class has been itself present inside java.util package concatenates one string to the end of another string. This method returns a string with the value of the string passed into the method, appended to the end of the string.

Illustration:

Input: String 1 : abc String 2 : def String n-1 : ... String n : xyz Output: abcdef...xyz

Example:

Java

class GFG {

`` public static void main(String args[])

`` {

`` String s = "Geeks " ;

`` s = s.concat( "for Geeks" );

`` System.out.println(s);

`` }

}

Now let us do well on the next concept where we will be discussing

‘+’ Operator

+ operator is used for concatenating strings on either side and is used just likely we are up to adding two numbers, henceforth providing us the flexibility to add on either side.

Example:

Java

class GFG {

`` public static void main(String args[])

`` {

`` String s1 = " Geeks " ;

`` String s2 = " for Geeks " ;

`` String s3 = s1 + s2;

`` String s4 = s2 + s1;

`` System.out.println(s3);

`` System.out.println(s4);

`` }

}

Output

Geeks for Geeks for Geeks Geeks

Although concat() method of Strings class and + operator are both used for the concatenation of strings, there are some differences between them which are depicted below in the tabular format as follows:

Factor 1: Number of arguments the concat() method and + operator takes

Example

Java

public class GFG {

`` public static void main(String[] args)

`` {

`` String s = "Geeks" , t = "for" , g = "geeks" ;

`` System.out.println(s + t + g);

`` System.out.println(s.concat(t));

`` }

}

Output

Geeksforgeeks Geeksfor

Factor 2: Type of arguments

Factor 3: concat() method raises java.lang.NullPointer Exception

Example

Java

public class GFG {

`` public static void main(String[] args)

`` {

`` String s = "Geeks" ;

`` String r = null ;

`` System.out.println(s + r);

`` System.out.println(s.concat(r));

`` }

}

Output:

Factor 4: Creates a new String object

Example

Java

public class GFG {

`` public static void main(String[] args)

`` {

`` String s = "Geeks" , g = "" ;

`` String f = s.concat(g);

`` if (f == s)

`` System.out.println( "Both are same" );

`` else

`` System.out.println( "not same" );

`` String e = s + g;

`` if (e == s)

`` System.out.println( "Both are same" );

`` else

`` System.out.println( "not same" );

`` }

}

Output

Both are same not same

Factor 5: Performance

concat() method is better than the + operator because it creates a new object only when the string length is greater than zero(0) but the + operator always creates a new string irrespective of the length of the string.

Conclusion: From above two programs we draw see that both are somehow adding up two strings directly or indirectly. In concat method we are bound to while adding strings as we have to pass the string in the parameter while in other case of ‘+’ operator, it is simply adding up strings likely mathematics, so there is no bound we can add either side.

Below is the table to depict major differences between both the methods:

Basics concat() Method + Operator
Number of arguments the concat() method and + operator takes Takes only one argument of string and concatenates it with another string. Takes any number of arguments and concatenates all the strings
Type of Arguments Takes only string arguments, if there is any other type is given in arguments then it will raise an error. Takes any type and converts to string type and then concatenates the strings.
NullPointer Exception Throws NullPointer Exception when the string is concatenated with null It does not raise any Exception when the string is concatenated with null
Creating a new String object Takes concatenates two strings and returns a new string object only if the length of the returned string is greater than the length of one of the concatenated strings, otherwise, it returns the same object. Creates a new string object every time irrespective of the length of the string.
Performance concat() method is better than the ‘+’ operator because it creates a new object only when the string length is greater than zero(0), so it uses less amount of memory. + operator always creates a new string irrespective of the length of string therefore it takes more memory.