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);JavaQuest
- The variable
pirmasstores the text Java. - The variable
antrasstores the text Quest. pirmas.concat(antras)appends theantrasvalue to the end of thepirmasvalue.- 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)
| Part | Meaning |
|---|---|
| Calling object | pirmas in the line pirmas.concat(antras) |
| Parameter | str – text to append at the end |
| Return type | String |
| Result | New concatenated text |
| Original text | Unchanged because String is immutable |
| Error | NullPointerExceptionwhen the argument is null |
concat() does not change the original string
String tekstas = "Java";
tekstas.concat("Quest");
System.out.println(tekstas);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);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);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);Ž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));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);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);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));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);
}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 originalStringeven 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 anint,doubleor another non-Stringvalue. - Passing a
nullargument to the method. concat()is used in large loops instead ofStringBuilder.
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().