Java String concat() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, concat() joins another string onto the end of this string and returns a new string. The original strings do not change.

You pass the string to append. It is required. Assign the result, or the new text is lost.

Syntax

Call concat on the first string. Start with this line.

java
String s = a.concat(b);
CallWhat it does
a.concat(b)Returns a new string with b added after a.

The + operator is more common in everyday Java. Prefer StringBuilder for many appends in a loop.

Library declarations
java
public String concat(String str);

Parameters

ParameterDescription
strRequired. The string to add at the end. Do not pass null.

Return Value

Return ValueDescription
New StringThe joined text. The original strings stay unchanged.

Java Examples

Example 1 — Basic Concatenation

Join two strings and confirm the original is unchanged.

java
public class ConcatBasic {
    public static void main(String[] args) {
        String first = "Hello";
        String second = " World";

        String result = first.concat(second);

        System.out.println(result);
        System.out.println(first);
    }
}

How It Works

  1. Two parts. first is "Hello"; second is " World".
  2. concat builds a new string. Characters are copied in order.
  3. Print result. You see Hello World.
  4. Print first. Still Hello — immutability in action.

Example 2 — concat vs + Operator

concat is string-only; + can mix in numbers.

java
public class ConcatVsPlus {
    public static void main(String[] args) {
        String name = "Java";
        int year = 2026;

        String withConcat = name.concat(" rocks");
        String withPlus = name + " " + year;

        System.out.println(withConcat);
        System.out.println(withPlus);
    }
}

How It Works

  1. concat joins strings only. Both sides must already be String.
  2. + converts year. The int becomes text and joins with name.
  3. Use concat for clarity. Explicit string-to-string append.
  4. Use + for mixed types. Numbers, chars, and other values.

Example 3 — Prefer StringBuilder in a Loop

Repeated concat works, but a builder is better for many pieces.

java
public class ConcatBuilder {
    public static void main(String[] args) {
        String[] words = { "one", "two", "three" };

        StringBuilder sb = new StringBuilder();
        for (String w : words) {
            if (sb.length() > 0) sb.append(" ");
            sb.append(w);
        }
        System.out.println(sb.toString());

        String joined = "";
        for (String w : words) {
            joined = joined.concat(w).concat(" ");
        }
        System.out.println(joined.trim());
    }
}

How It Works

  1. StringBuilder grows one buffer. Appends words without copying every time.
  2. Spaces between words. Added only when the builder is non-empty.
  3. concat loop also works. But each call allocates a new string.
  4. Prefer a builder for many joins. Or use String.join.

Frequently Asked Questions

concat() joins another string onto the end and returns a new string.
str is required. It is the string to append.
A new String with the joined text.
No. Strings are immutable. Assign the result.
Both join text. + is more common. Use StringBuilder for many appends in a loop.

Next: contains()

Check whether a string contains a given substring.

contains() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful