String concat() method - Syntax and Examples
Learn about the Java String concat() method. This tutorial covers syntax, parameters, return value and provides examples of how to use it.
This reference explains String concat() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.
Use this reference
Identify the documented behavior, read a complete example and check its inputs before applying it.
String concat() method - Syntax and Examples
Introduction
The concat() method in Java is a straightforward way to join two strings together. Think of it like physically attaching two pieces of string – the result is one longer string containing both original sequences.
Syntax
pubblic String concat(String str)
Parameters
| Parameter | Description |
|---|---|
str |
The string to be concatenated with the current string. It can be null, in which case it is treated as an empty string. |
Return Value
This method returns a new string that is the concatenation of the sequence characters of the current string and the string argument.
Examples
Example 1: Basic Concatenation
Let's start with a simple example. We have two strings, "Hello" and " World", and we want to combine them using concat().
String str1 = "Hello";
String str2 = " World";
String result = str1.concat(str2);
System.out.println(result);
Hello World
In this example, str1.concat(str2) creates a new string that is "Hello" followed by " World". The result is then printed to the console.
Example 2: Concatenating with an Empty String
What happens if you try to concatenate with an empty string? It doesn’t change the original string, but it still returns a new one.
String str1 = "Java";
String str2 = ""; // An empty string
String result = str1.concat(str2);
System.out.println("Original: "+str1 +", Result:"+result);
Original: Java, Result:Java
Here, concatenating "Java" with an empty string doesn't modify the original string. The result is still "Java". This highlights that concat() always returns a *new* string.
Example 3: Concatenating Multiple Strings
You can chain multiple calls to concat() together if you want to combine more than two strings.
String str1 = "Hello";
String str2 = " ";
String str3 = "World";
String result = str1.concat(str2).concat(str3);
System.out.println(result);
Hello World
In this case, we first concatenate str1 with str2 (resulting in " Hello "), and then we concatenate the result with str3 to get "Hello World". This demonstrates how you can build up strings step-by-step.
Example 4: Handling Null Values
The concat() method handles null values gracefully. If you try to concatenate with a null string, it's treated as an empty string.
String str1 = "Hello";
String str2 = null;
String result = str1.concat(str2);
System.out.println(result);
Hello
Because str2 is null, it's treated as an empty string when concatenated with “Hello”, so the output remains “Hello”.
Using this reference
Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.
Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.
Supporting documentation: Official Java learning guide.
Reference checks are optional and do not count toward course progress.