Difference between concat() and + operator in Java
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.
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
- concat() method takes only one argument of string and concatenates it with other string.
- + operator takes any number of arguments and concatenates all the strings.
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
- string concat() method takes only string arguments, if there is any other type is given in arguments then it will raise an error.
- + operator takes any type and converts to string type and then concatenates the strings.
Factor 3: concat() method raises java.lang.NullPointer Exception
- concat() method throws NullPointer Exception when a string is concatenated with null
- + operator did not raise any Exception when the string is concatenated with null.
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
- concat() method takes concatenates two strings and returns a new string object only string length is greater than 0, otherwise, it returns the same object.
- + operator creates a new String object every time irrespective of the length of the string.
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. |
Similar Reads
Difference Between Length and Capacity in Java
Length and capacity are two different things in Java. Length basically increases when characters are appended to the string whereas the capacity increases when the current capacity is exceeded by a new length. length () method is a part of the Java String class. It is used to find the length of the
3 min read
Difference between List, Set and Map in Java
List interface in Java is a sub-interface of the Java collections interface. It contains the index-based methods to insert, update, delete, and search the elements. It can have duplicate elements also. We can also store the null elements in the list. List preserves the insertion order, it allows pos
4 min read
Difference between String and Character array in Java
Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties. Differences between Strings and Character Arrays: PropertyStringCharacter ArrayDefinitionA sequence of characters is
3 min read
Difference between Unary and Binary Operators
Unary Operators and Binary operators are both fundamental concepts in computer science and programming languages, especially in the context of arithmetic and logical operations. Here's a breakdown of the differences between them: Unary Operators:Unary Operator is an operator that operates on a singl
2 min read
Difference between x++ and x=x+1 in Java
In x++, it increase the value of x by 1 and in x=x+1 it also increase the value of x by 1. But the question is that both are same or there is any difference between them. We should aware with the fact that whenever we are trying to apply any arithmetic operator between two variables a and b, the res
3 min read
Difference between Simple and Compound Assignment in Java
Many programmers believe that the statement "x += i" is simply a shorthand for "x = x + i". This isnât quite true. Both of these statements are assignment expressions. The second statement uses the simple assignment operator (=), whereas the first uses a compound assignment operator. The compound as
2 min read
Difference between the Constructors and Methods
Java is a pure OOPS concept based programming language. Hence in Java, all the variables, data and the statements must be present in classes. These classes consist of both constructors and methods. Methods and Constructors are different from each other in a lot of ways. Constructors: Constructors ar
3 min read
Difference Between '+' and 'append' in Python
+ Operator creates a new sequence by concatenating two existing sequences and .append() method (available for lists) modifies the existing list in place by adding one item at the end. In this article we are going to compare '+' operator and append in Python. + Operator in Python+ operator is typical
3 min read
Addition and Concatenation Using + Operator in Java
Till now in Java, we were playing with the integral part where we witnessed that the + operator behaves the same way as it was supposed to because the decimal system was getting added up deep down at binary level and the resultant binary number is thrown up at console in the generic decimal system.
2 min read
Compound Assignment Operators in Java
In Java, compound-assignment operators provide a shorter syntax for assigning the result of an arithmetic or bitwise operator. They perform the operation on two operands before assigning the result to the first operand. The following are all possible assignment operators in Java: 1. += (compound add
7 min read