Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

String concatenation in Java is one of the most common operations. In this tutorial, we’ll walk through some approaches to string concatenation, with a focus on describing how to use concat() and the “+” operator approaches. Finally, we’ll discuss how to select the correct one depending on what we need to do.

2. Approaches to Concatenation

In general, there are different approaches to concatenating two or more strings in Java. Let’s look at some examples with a description of each one.

2.1. Using the “+” Operator

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. We can also use it for concatenating more than two strings.

Let’s see a code example:

@Test
void whenUsingPlusOperatorANull_thenAssertEquals() {
    String stringOne = "Hello ";
    String stringTwo = null;
    assertEquals("Hello null", stringOne + stringTwo);
}

The compiler internally transforms the”+” operator to a StringBuilder (or StringBuffer) class and its append() method.

Since the “+” operator silently converts the argument to a String (using the toString() method for objects), we avoid the NullPointerException. However, we need to consider if our final string result works for us with the “null” in the string body.

2.2. Using the concat() Method

The concat() method in the String class appends a specified string at the end of the current string, and returns the new combined string. Given that the String class is immutable, the original String isn’t changed.

Let’s test this behavior:

@Test
void whenUsingConcat_thenAssertEquals() {
    String stringOne = "Hello";
    String stringTwo = " World";
    assertEquals("Hello World", stringOne.concat(stringTwo));
}

In the previous example, the stringOne variable is the base string. With the concat() method, stringTwo is appended at the end of stringOne. The concat() operation is immutable, so we need an explicit assignment. The next example illustrates this case:

@Test
void whenUsingConcatWithOutAssignment_thenAssertNotEquals() {
    String stringOne = "Hello";
    String stringTwo = " World";
    stringOne.concat(stringTwo);
    assertNotEquals("Hello World", stringOne); // we get only Hello
}

Additionally, to get our final concatenated string, we’ll need to assign the concat() result to a variable:

stringOne = stringOne.concat(stringTwo);
assertEquals("Hello World", stringOne);

Another useful feature of concat() is when we need to concatenate multiple String objects, which this method allows. Moreover, we can also append space and special characters:

@Test
void whenUsingConcatToMultipleStringConcatenation_thenAssertEquals() {
    String stringOne = "Hello";
    String stringTwo = "World";
    String stringThree = ", in Jav";
    stringOne = stringOne.concat(" ").concat(stringTwo).concat(stringThree).concat("@");
    assertEquals("Hello World, in Jav@", stringOne);
}

What about nulls? Neither the current string, nor the string to be appended can be null values. Otherwise, the concat() method throws a NullPointerException:

@Test
void whenUsingConcatAppendANull_thenAssertEquals() {
    String stringOne = "Hello";
    String stringTwo = null;
    assertThrows(NullPointerException.class, () -> stringOne.concat(stringTwo));
}

2.3. StringBuilder Class

First, we have the StringBuilder class. This class provides the append() method to perform concatenation operations. The next example shows us how it works:

@Test
void whenUsingStringBuilder_thenAssertEquals() {
    StringBuilder builderOne = new StringBuilder("Hello");
    StringBuilder builderTwo = new StringBuilder(" World");
    StringBuilder builder = builderOne.append(builderTwo);
    assertEquals("Hello World", builder.toString());
}

On the other hand, a similar concatenation approach is the StringBuffer class. Contrary to the StringBuilder, which is non-synchronized (i.e., not thread-safe), StringBuffer is synchronized (i.e., thread-safe). However, it has worse performance than StringBuilder. It also has an append() method just like StringBuilder does.

2.4. String format() Method

Another way to perform string concatenation is using the format() method in the String class. Using format specifiers like %s, we can concatenate multiple strings by their string value or object:

@Test
void whenUsingStringFormat_thenAssertEquals() {
    String stringOne = "Hello";
    String stringTwo = " World";
    assertEquals("Hello World", String.format("%s%s", stringOne, stringTwo));
}

2.5. Approaches to Concatenation in Java 8 and Above

The method join() in the String class, for Java 8 and above, can perform string concatenation. In this case, the method takes as the first argument a delimiter used between the strings that’ll be concatenated:

@Test
void whenUsingStringJoin_thenAssertEquals() {
    String stringOne = "Hello";
    String stringTwo = " World";
    assertEquals("Hello World", String.join("", stringOne, stringTwo));
}

As of Java 8, the StringJoiner class was added. This class joins Strings using delimiter, prefix, and suffix. The following code snippet is an example of its use:

@Test
void whenUsingStringJoiner_thenAssertEquals() {
    StringJoiner joiner = new StringJoiner(", ");
    joiner.add("Hello");
    joiner.add("World");
    assertEquals("Hello, World", joiner.toString());
}

With the addition of the Stream API in Java 8, we can find Collectors. The Collectors class has the joining() method. This method works similarly to the join() method in the String class. It’s used for collections. The following example code snippet shows us how it works:

@Test
void whenUsingCollectors_thenAssertEquals() {
    List<String> words = Arrays.asList("Hello", "World");
    String collect = words.stream().collect(Collectors.joining(", "));
    assertEquals("Hello, World", collect);
}

3. Choosing an Approach

Finally, if we need to choose between the concat() method and the “+” operator, we need to consider some aspects.

First, the concat() method only accepts strings. Meanwhile, the “+” operator takes any type and converts it to a string. On the other hand, the concat() method raises a NullPointerExeption on null values, which is not the case with the “+” operator.

Moreover, there’s a performance difference between them. The concat() method performs better than the “+” operator. The latter always creates a new string irrespective of the length of the string. Additionally, we need to take into account that the concat() method only creates a new string when the string to be appended has a length greater than 0. Otherwise, it returns the same object.

4. Conclusion

In this article, we provided a quick overview of string concatenation in Java. Additionally, we discussed in detail the use of concat() and the “+” operator to perform string concatenations. Finally, we performed a comparative analysis between the concat() method and the “+” operator to determine how to choose between them in different contexts.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

2 Comments
Oldest
Newest