No, not quite.

Firstly, there's a slight difference in semantics. If a is null, then a.concat(b) throws a NullPointerException but a+=b will treat the original value of a as if it were null. Furthermore, the concat() method only accepts String values while the + operator will silently convert the argument to a String (using the toString() method for objects). So the concat() method is more strict in what it accepts.

To look under the hood, write a simple class with a += b;

public class Concat {
    String cat(String a, String b) {
        a += b;
        return a;
    }
}

Now disassemble with javap -c (included in the Sun JDK). You should see a listing including:

java.lang.String cat(java.lang.String, java.lang.String);
  Code:
   0:   new     #2; //class java/lang/StringBuilder
   3:   dup
   4:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":()V
   7:   aload_1
   8:   invokevirtual   #4; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   11:  aload_2
   12:  invokevirtual   #4; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   15:  invokevirtual   #5; //Method java/lang/StringBuilder.toString:()Ljava/lang/    String;
   18:  astore_1
   19:  aload_1
   20:  areturn

So, a += b is the equivalent of

a = new StringBuilder()
    .append(a)
    .append(b)
    .toString();

The concat method should be faster. However, with more strings the StringBuilder method wins, at least in terms of performance.

The source code of String and StringBuilder (and its package-private base class) is available in src.zip of the Sun JDK. You can see that you are building up a char array (resizing as necessary) and then throwing it away when you create the final String. In practice memory allocation is surprisingly fast.

Update: As Pawel Adamski notes, performance has changed in more recent HotSpot. javac still produces exactly the same code, but the bytecode compiler cheats. Simple testing entirely fails because the entire body of code is thrown away. Summing System.identityHashCode (not String.hashCode) shows the StringBuffer code has a slight advantage. Subject to change when the next update is released, or if you use a different JVM. From @lukaseder, a list of HotSpot JVM intrinsics.

Answer from Tom Hawtin - tackline on Stack Overflow
🌐
W3Schools
w3schools.com › java › ref_string_concat.asp
Java String concat() Method
The concat() method appends (concatenate) a string to the end of another string.
Top answer
1 of 12
655

No, not quite.

Firstly, there's a slight difference in semantics. If a is null, then a.concat(b) throws a NullPointerException but a+=b will treat the original value of a as if it were null. Furthermore, the concat() method only accepts String values while the + operator will silently convert the argument to a String (using the toString() method for objects). So the concat() method is more strict in what it accepts.

To look under the hood, write a simple class with a += b;

public class Concat {
    String cat(String a, String b) {
        a += b;
        return a;
    }
}

Now disassemble with javap -c (included in the Sun JDK). You should see a listing including:

java.lang.String cat(java.lang.String, java.lang.String);
  Code:
   0:   new     #2; //class java/lang/StringBuilder
   3:   dup
   4:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":()V
   7:   aload_1
   8:   invokevirtual   #4; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   11:  aload_2
   12:  invokevirtual   #4; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   15:  invokevirtual   #5; //Method java/lang/StringBuilder.toString:()Ljava/lang/    String;
   18:  astore_1
   19:  aload_1
   20:  areturn

So, a += b is the equivalent of

a = new StringBuilder()
    .append(a)
    .append(b)
    .toString();

The concat method should be faster. However, with more strings the StringBuilder method wins, at least in terms of performance.

The source code of String and StringBuilder (and its package-private base class) is available in src.zip of the Sun JDK. You can see that you are building up a char array (resizing as necessary) and then throwing it away when you create the final String. In practice memory allocation is surprisingly fast.

Update: As Pawel Adamski notes, performance has changed in more recent HotSpot. javac still produces exactly the same code, but the bytecode compiler cheats. Simple testing entirely fails because the entire body of code is thrown away. Summing System.identityHashCode (not String.hashCode) shows the StringBuffer code has a slight advantage. Subject to change when the next update is released, or if you use a different JVM. From @lukaseder, a list of HotSpot JVM intrinsics.

2 of 12
105

Niyaz is correct, but it's also worth noting that the special + operator can be converted into something more efficient by the Java compiler. Java has a StringBuilder class which represents a non-thread-safe, mutable String. When performing a bunch of String concatenations, the Java compiler silently converts

String a = b + c + d;

into

String a = new StringBuilder(b).append(c).append(d).toString();

which for large strings is significantly more efficient. As far as I know, this does not happen when you use the concat method.

However, the concat method is more efficient when concatenating an empty String onto an existing String. In this case, the JVM does not need to create a new String object and can simply return the existing one. See the concat documentation to confirm this.

So if you're super-concerned about efficiency then you should use the concat method when concatenating possibly-empty Strings, and use + otherwise. However, the performance difference should be negligible and you probably shouldn't ever worry about this.

🌐
Baeldung
baeldung.com › home › java › java string › string concatenation in java
String Concatenation in Java | Baeldung
January 8, 2024 - One of the most common concatenation approaches in Java is using the “+” operator. The “+” operator provides more flexibility for string concatenation over other approaches. First, it doesn’t throw any exceptions for null values. Second, it converts null into its string representation.
🌐
TechVidvan
techvidvan.com › tutorials › java-string-concatenation
Java String concat() Method with Examples - TechVidvan
February 17, 2025 - Concatenation is combining two or more strings to form a new string by appending the next string to the end of the previous strings. In Java, two strings can be concatenated using the + or += operator or the concat() method, defined in the ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-string-concat-examples
Java String concat() Method with Examples - GeeksforGeeks
The concat() method in Java is used to append one string to another and returns a new combined string.
Published: April 7, 2026
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › String.html
String (Java Platform SE 8 )
July 21, 2026 - The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the ...
🌐
Coderanch
coderanch.com › t › 779364 › java › concat-Strings
+= and .concat() for Strings (Beginning Java forum at Coderanch)
February 2, 2024 - The concat() method is not directly changing the string, because the String class is written to be immutable - none of its methods change the string data. Instead concat() is creating a new string, just like += ... except that += is also re-assigning that new string to the left-hand variable, ...
Find elsewhere
🌐
CodeGym
codegym.cc › java blog › strings in java › string concatenation in java
String Concatenation in Java
April 1, 2025 - I learn concatenation in Java CodeGym.cc/quest I have got it I haven't got it It’s important to know that the concat() method doesn’t change the string, but creates a new one as a result of merging the current one and the one passed as a parameter.
🌐
Educative
educative.io › answers › how-to-concatenate-strings-in-java
How to concatenate strings in Java
The += operator modifies the original string variable by adding another string to it. ... Note: As per the official Java documentation, when you use + or += to concatenate a string with a reference to an object in Java, the reference will ...
🌐
Medium
medium.com › @dr4wone › the-battle-of-string-concatenation-in-java-which-method-reigns-supreme-86d07d5d0525
The Battle of String Concatenation in Java: Which Method Reigns Supreme? | by Daniel Baumann | Medium
April 9, 2023 - String concatenation is a common task in programming, and in Java, there are two ways to do it: using the + operator or the StringBuilder class.
🌐
W3Schools
w3schools.com › java › java_strings_concat.asp
Java Strings Concatenation
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... The + operator can be used between strings to combine them.
🌐
Baeldung
baeldung.com › home › java › java string › concatenating strings in java
Concatenating Strings in Java | Baeldung
May 8, 2025 - In this example, our chain is started with a String literal, the concat method then allows us to chain the calls to append further Strings. Next up is the String.format method, which allows us to inject a variety of Java Objects into a String template.
🌐
CodeSignal
codesignal.com › learn › courses › getting-started-with-java › lessons › java-string-magic-understanding-concatenation-operations
Java String Magic: Understanding Concatenation Operations
The concat method joins the strings in a manner similar to the '+' operator, but it's designed solely for strings. Remember - String instances are immutable in Java, once created, the string cannot be changed. However, there are some nifty tools provided by Java, e.g., StringBuilder is a mutable ...
🌐
CODEDOST
codedost.com › home › java › basic java programs › java program to concatenate two strings using string method concat()
JAVA program to concatenate two strings using string method concat() | CODEDOST
August 7, 2018 - For example str1=’code’ and str2=’dost’ then on concatenation the string would be ‘codedost’. Syntax str1.concat(str2) appends contents of str2 to str1 · import java.util.*; class stringConcatF { public static void main(String args[]) { String str1,str2; Scanner sc = new Scanner(System.in); System.out.println("Enter the 1st string"); str1=sc.nextLine(); System.out.println("Enter the 2nd string"); str2=sc.nextLine(); System.out.println("Concatenated String is "); System.out.println(str1.concat(str2)); } }
🌐
Moss GU
mossgreen.github.io › Concatenating-Java-Strings
Concatenating Java Strings | Moss GU
March 13, 2018 - If you declare multiple strings and use “+” concatenating them together.
🌐
Quora
venkateshonjava.quora.com › String-Concatenation-in-Java
String Concatenation in Java - Java 27 - Quora
There are 2 ways to concat string in java: 1. + (String concatenation) operator 2. contact() method Java string concatenation operator (+) is used to add strings. For example, class TestStringConcat { public static void main(String[] args) { String s = “Sachin” + “Tendulkar”; System.out.p...
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String
String - JavaScript | MDN
They are of limited use, as they are based on a very old HTML standard and provide only a subset of the currently available HTML tags and attributes. Many of them create deprecated or non-standard markup today. In addition, they do string concatenation without any validation or sanitation, which makes them a potential security threat when directly inserted using innerHTML.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Template_literals
Template literals (Template strings) - JavaScript | MDN
In addition, the String.raw() method exists to create raw strings just like the default template function and string concatenation would create.