You can concatenate Strings using the + operator:

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

java - String concatenation: concat() vs "+" operator - Stack Overflow
Niyaz is correct, but it's also ... + 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 ... which for large strings is significantly more efficient. As far as I know, this does not happen when you use the concat ... More on stackoverflow.com
🌐 stackoverflow.com
Java String Concatenation using '+' operator - Stack Overflow
What does actually happens when we concatenate a string S2 of size Y to a string S1 of size X ( already present on the heap) using the + operator? ... If S1 was present in the string pool (which is stored in the heap) and we execute the concat method, then the method gets executed on the stack. So, the CPU will need to copy the S1 in the stack => READ S1 · As strings are immutable, Java ... More on stackoverflow.com
🌐 stackoverflow.com
[Java] How is the running time of string concatenation O(n^2)?
Roughly speaking, you can think of strings as immutable arrays of characters. Then, "Hello" + "World" would be equivalent to taking the char array ['H', 'e', 'l', 'l', 'o'], and the char array ['W', 'o', 'r', 'l', 'd'], and then creating a new array (of length 10) with the combined letters in both inputs. Therefore, concatenating a string of length n and a string of length m takes O(n + m) time. That said, it is worth noting that for compile-time constants, any decent compiler will precompute concatenated strings. In addition, modern compilers will generally also optimize a fixed amount of string concatenations. This basically just leaves the advice "don't concatenate strings in a loop". More on reddit.com
🌐 r/learnprogramming
7
30
July 25, 2016
Is it "ok" to use the ternary operator in a string like so?
It's a style choice. Personally I think it looks messy (especially with all the +s - doesn't Java or C# or whatever this is have better string formatting syntax?). Other people really really like one-liners. More on reddit.com
🌐 r/learnprogramming
10
6
April 2, 2023
🌐
CodeSignal
codesignal.com › learn › courses › getting-started-with-java › lessons › java-string-magic-understanding-concatenation-operations
Java String Magic: Understanding Concatenation Operations
String firstName = "Neil"; String lastName = "Armstrong"; String fullName = firstName + " " + lastName; // Concatenation operation System.out.println(fullName); // Output: Neil Armstrong · The '+' operator joins firstName, a space, and lastName to form the fullName string.
🌐
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. You can provide either a variable, a number, or a String literal (which is always surrounded by double quotes).
🌐
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.
🌐
Educative
educative.io › answers › how-to-concatenate-strings-in-java
How to concatenate strings in Java - Educative.io
The program below demonstrates how to concatenate two strings using the + or += operator in Java. The += operator modifies the original string variable by adding another string to it.
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.

Find elsewhere
🌐
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.
🌐
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....
🌐
DEV Community
dev.to › arshisaxena26 › understanding-the-operator-in-java-string-concatenation-arithmetic-and-common-pitfalls-59bm
Understanding the + Operator in Java: String Concatenation, Arithmetic, and Common Pitfalls - DEV Community
January 20, 2025 - In addition to performing arithmetic, the + operator is also overloaded to concatenate strings. This means that when one of the operands is a String, Java converts the other operand to a String and concatenates the two.
🌐
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. So the method returns a new String object, that’s why you can create such long chains of String concat. You can use these two operators in the same way as with numbers.
🌐
Tpoint Tech
tpointtech.com › string-concatenation-in-java
String Concatenation in Java - Tpoint Tech
March 17, 2025 - The simplest and most commonly used method for concatenating strings is using the + operator. We can concatenate two or more strings by simply using the + operator between them. ... The Java compiler internally manipulates the string by using the following statement.
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-concat-and-operator-in-java
Difference between concat() and + operator in Java - GeeksforGeeks
July 11, 2025 - 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.
🌐
Medium
medium.com › @AlexanderObregon › beginners-guide-to-java-string-concatenation-1e2fbccda0bc
Beginner’s Guide to Java String Concatenation
March 26, 2024 - It's highly readable and easily understood, making it an excellent choice for beginners or for code that isn't performance-critical. The + operator can be used to join strings with other data types, as Java automatically converts the non-string ...
🌐
TutorialsPoint
tutorialspoint.com › article › Different-ways-to-concatenate-Strings-in-Java
Different ways to concatenate Strings in Java
You can concatenate two strings in Java either by using the concat() method or by using the ‘+’ , the “concatenation” operator.
🌐
PrepBytes
prepbytes.com › home › java › string concatenation in java
String Concatenation in Java
July 3, 2024 - In this example, we demonstrate three different methods of string concatenation. First, we use the + operator to concatenate str1, a space, and str2, resulting in the string "Hello World".
🌐
TechVidvan
techvidvan.com › tutorials › java-string-concatenation
Java String concat() Method with Examples - TechVidvan
February 17, 2025 - The program below demonstrates how to concatenate two strings using the + operator in Java.
Top answer
1 of 2
2

in the doc you can read:

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.

a = a + b is the equivalent of a += b or:

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

Disclaimer: The String class has undergone multiple changes to improve performance and space utilization. What happens when JIT compiles code is then entirely undefined. The following is a simplification, and ignores any optimizations that may or may not be applied.

String is a class that encapsulates a char[]. The array length is always exactly the length() of the string. The class, and the underlying array, is immutable.

class String {
    private final char[] arr;
}

StringBuilder (and StringBuffer) is another class that encapsulates a char[], but the array is almost always larger than the number of characters in the array. The class, and the array, is mutable.

class StringBuilder {
    private char[] arr;
    private int len;
}

When you do string concatenation with the + operator, the compiler generates that as:

// Java code
s = s1 + s2 + s3;

// Generated code
s = new StringBuilder().append(s1).append(s2).append(s3).toString();

StringBuilder will initially create the array with length 16, and will re-allocate the array when needed. Worst case is that s1, s2, and s3 are all too large for the current array, so each append() call needs to re-size the array.

This means that the would progress as follows:

  1. new StringBuilder() - Creates char[16].

  2. append(s1) - Resizes arr, then copies chars from s1.arr to the array.

  3. append(s2) - Resizes arr, copies existing content (chars from s1) to new array, then copies chars from s2.arr to the array.

  4. append(s3) - Resizes arr, copies existing content (chars from s1 and s2) to new array, then copies chars from s3.arr to the array.

  5. toString() - Create new String with char[] sized to exactly fit the characters in the StringBuilder, then copies the content (chars from s1, s2, and s3) to the new String.

All-in-all the chars from s1 ends up being copied 4 times.

If the string concatenation is S1 + S2, like in the question, then the characters from S1 are copied 2 or 3 times, and the characters from S2 are copied 2 times.

Since time complexity is generally worst case, that means O(3m + 2n), not the O(2m + n) suggested in the question. Of course, Big-O eliminates constant factors, so it is actually O(m + n).

🌐
iO Flood
ioflood.com › blog › java-string-concatenation
Java String Concatenation | Complete Method Guide
July 8, 2024 - Java String Tutorial by DigitalOcean tutorial offers a guide on various operations you can perform with strings in Java. Baeldung’s Guide on Java Strings covers a wide range of topics related to strings in Java, including concatenation. In this comprehensive guide, we’ve journeyed through the world of Java string concatenation, exploring its various methods, their usage, and the common issues you might encounter. We started with the basics, learning how to use the ‘+’ operator for simple string concatenation.
🌐
Deep Java
digibeatrix.com › home › data handling & collections (java standard library) › java string concatenation explained: best methods, performance, and best practices
Java String Concatenation Explained: Best Methods, Performance, and Best Practices - Deep Java
December 29, 2025 - The most intuitive and straightforward method is using the + operator: String firstName = "Taro"; String lastName = "Yamada"; String fullName = firstName + " " + lastName; In Java 8 and later, concatenation using the + operator is internally ...