You can concatenate Strings using the + operator:

Use with AISystem.out.println("Your number is " + theNumber + "!");

theNumber is implicitly converted to the String "42".

Answer from Peter Lang on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_strings_concat.asp
Java Strings Concatenation
The + operator can be used between strings to combine them.
🌐
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.
🌐
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. It does not modify the original string since strings are immutable. ... class GFG { public static void main(String args[]) { // String ...
Published: April 7, 2026
🌐
W3Schools
w3schools.com › java › ref_string_concat.asp
Java String concat() Method
Java Examples Java Videos Java ... System.out.println(firstName.concat(lastName)); ... The concat() method appends (concatenate) a string to the end of another string....
🌐
Baeldung
baeldung.com › home › java › java string › concatenating strings in java
Concatenating Strings in Java | Baeldung
May 8, 2025 - Although synchronization is often synonymous with thread safety, it’s not recommended for use in multithreaded applications due to StringBuffer’s builder pattern. While individual calls to a synchronized method are thread safe, multiple calls are not. Next up is the addition operator (+). This is the same operator that results in the addition of numbers and is overloaded to concatenate when applied to Strings.
🌐
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.
🌐
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 ...
Find elsewhere
🌐
Educative
educative.io › answers › how-to-concatenate-strings-in-java
How to concatenate strings in Java
StringBuilder class is an efficient way to concatenate strings in Java. It allows us to modify strings without creating intermediate string objects. We can use the StringBuilder class to create the first string and then append it to another ...
🌐
Java Guides
javaguides.net › 2019 › 01 › concatenate-strings-in-java-6-ways-concatenate-strings-in-java.html
How to Concatenate Strings in Java
June 15, 2024 - The + operator is the simplest and most commonly used method for string concatenation in Java.
🌐
Vultr Docs
docs.vultr.com › java › standard library › java › lang › string › concat()
Java String concat() - Concatenate Strings | Vultr Docs
November 19, 2024 - The concat() method in Java is designed for concatenating one string to the end of another, making it a fundamental tool for string manipulation in Java programming.
🌐
Tpoint Tech
tpointtech.com › string-concatenation-in-java
String Concatenation in Java - Tpoint Tech
August 21, 2026 - In Java, String concatenation is implemented through the StringBuilder (or StringBuffer) class and its append() method. String concatenation operator produces a new String by appending the second operand onto the end of the first operand.
🌐
Medium
medium.com › @AlexanderObregon › beginners-guide-to-java-string-concatenation-1e2fbccda0bc
Beginner’s Guide to Java String Concatenation
March 26, 2024 - String concatenation in Java is a fundamental concept that plays a crucial role in nearly every Java application, from simple console programs to complex web applications. At its core, string concatenation is the process of combining two or more strings end-to-end to form a new string.
🌐
Programiz
programiz.com › java-programming › library › string › concat
Java String concat() | Programiz
March 12, 2024 - In Java, you can also use the + operator to concatenate two strings.
🌐
LabEx
labex.io › tutorials › java-java-string-concatenation-117956
Mastering String Concatenation in Java | LabEx
Another way to concatenate strings in Java is by using the StringBuilder class. The StringBuilder class provides append() method to combine two or more strings. StringBuilder is faster than String class while concatenating strings.
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.

🌐
Blogger
javarevisited.blogspot.com › 2015 › 01 › 3-examples-to-concatenate-string-in-java.html
3 Examples to Concatenate String in Java
You can use this operator even ... String "123". The other two ways to concatenate String in Java is by using StringBuffer and StringBuilder....
🌐
ThoughtCo
thoughtco.com › concatenation-2034055
Understanding the Concatenation of Strings in Java
May 18, 2025 - Using the + operator is the most common way to concatenate two strings in Java.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › string_concat.htm
Java - String concat() Method
The Java String concat() method is used to concatenate the specified string to the end of this string. Concatenation is the process of combining two or more strings to form a new string.