STRING INSTANCE METHODS

Java String concat() method

concat() appends one string to another and returns a new String value. Since Java String objects are immutable, the original text is not changed.

Joining two strings

String pirmas = "Java";
String antras = "Quest";

String rezultatas = pirmas.concat(antras);

System.out.println(rezultatas);
Console output

JavaQuest

  1. The variable pirmas stores the text Java.
  2. The variable antras stores the text Quest.
  3. pirmas.concat(antras) appends the antras value to the end of the pirmas value.
  4. The method returns the new text JavaQuest.

Why is concat() an instance method?

concat() is called on a specific string because another string is appended to that object's value:

String rezultatas = pirmas.concat(antras);

We therefore do not write String.concat(...). The left-hand pirmas object is the text to which the argument is appended.

Method syntax

public String concat(String str)

Parts of the concat() method
PartMeaning
Calling objectpirmas in the line pirmas.concat(antras)
Parameterstr – text to append at the end
Return typeString
ResultNew concatenated text
Original textUnchanged because String is immutable
ErrorNullPointerExceptionwhen the argument is null

concat() does not change the original string

String tekstas = "Java";

tekstas.concat("Quest");

System.out.println(tekstas);
Console output

Java

concat() returns a new value but does not update the variable automatically. If the result is not assigned, the combined text is lost.

Assign the result

String tekstas = "Java";

tekstas = tekstas.concat("Quest");

System.out.println(tekstas);
Console output

JavaQuest

To use the combined text later, store it in a variable. You can reuse the same variable or create a new one.

Spaces are not added automatically

String pirmas = "Java";
String antras = "Quest";

String rezultatas = pirmas.concat(" ").concat(antras);

System.out.println(rezultatas);
Console output

Java Quest

concat() appends exactly the supplied text. Add any required space between words yourself: concat(" ").

The difference from the + operator

String vardas = "Jonas";
int taskai = 120;

System.out.println("Žaidėjas: ".concat(vardas));
System.out.println("Taškai: " + taskai);
Console output

Žaidėjas: Jonas
Taškai: 120

concat() accepts only a String argument. + operator conveniently joins text with other values, such as int. For everyday text formatting, + is therefore often simpler.

Empty strings

String tekstas = "Java";

System.out.println(tekstas.concat(""));
System.out.println("".concat(tekstas));
Console output

Java
Java

Appending an empty string leaves the visible text unchanged. An empty string can also be the calling object to which other text is appended.

Chaining concat()

String rezultatas = "Java"
    .concat(" ")
    .concat("programavimo")
    .concat(" ")
    .concat("pamoka");

System.out.println(rezultatas);
Console output

Java programavimo pamoka

Because concat() returns a new String, so you can immediately call another concat()on the result. This works, but longer text is often clearer with StringBuilder, String.join() or formatting.

concat() in a loop

String rezultatas = "";

for (int i = 1; i <= 3; i++) {
  rezultatas = rezultatas.concat("Java ");
}

System.out.println(rezultatas);
Console output

Java Java Java

In a loop, concat() creates a new String object each time. This is acceptable in small examples, but repeated concatenation is more efficient with StringBuilder.

The argument cannot be null

String tekstas = "Java";
String priedas = null;

System.out.println(tekstas.concat(priedas));
Result

NullPointerException

If concat() receives null, Java cannot append nonexistent text. It throws NullPointerException.

Checking safely before concatenation

String tekstas = "Java";
String priedas = null;

if (priedas != null) {
  System.out.println(tekstas.concat(priedas));
} else {
  System.out.println(tekstas);
}
Console output

Java

If the appended string may be null, check it first. This avoids a NullPointerException and can leave the original text unchanged.

Common mistakes

  • Expecting concat() to change the original Stringeven though it returns a new value.
  • Not assigning the result to a variable.
  • Forgetting to add a space between words.
  • To concat() trying to pass an int, double or another non- String value.
  • Passing a null argument to the method.
  • concat() is used in large loops instead of StringBuilder.

When should you use concat()?

concat() Use it to explicitly append one String to another when both are already String values. When joining many values, including numbers, or needing clearer formatting, more convenient choices are often +, StringBuilder, String.join() or String.format().