Java String concat() Method with Examples
Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
The Java concat() method is handy for putting strings together. It helps us create dynamic text in our programs. In this article, we’ll take a close look at how the concat() method works, and we’ll show you some practical examples.
The Java String concat() method joins two strings to make a new one. It’s great for when you want to add one string to another.
For example, you can use it to combine a first name and a last name to make a full name or to mix text with data from a database. When you understand how to use concat(), you have more control over the text in your programs.
Method Description
The Java concat() method description is defined as follows:
public String concat(String str)
It takes in a single String parameter and returns a new String. The original String that concat() is called on remains unchanged or immutable. The new String returned combines the original String and the String parameter passed in.
Under the hood, the concat() method creates a new StringBuilder, appends the string parameters, and converts the StringBuilder to a String. We’ll explore this further in the next section.
Internal Implementation
Here is a snippet from the Java OpenJDK source code showing how concat() is implemented:
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}As we can see, a new character array is created with enough capacity to hold both strings. The contents of the original string are copied over first using System.arraycopy(). Then, the contents of the parameter string are copied into the array starting at the end of the original string. This new array with the combined contents is passed to the String constructor to create the final concatenated string.
Following this process, strings remain immutable while allowing new String instances to be created efficiently under the hood.
Java String concat() Method Examples
Let’s explore some code examples to demonstrate how concat() can be applied.
Example 1: Immutable Nature
public class StringConcatenationExample {
public static void main(String[] args) {
String name = "John";
String fullName = name.concat(" Doe");
System.out.println(name); // Output: John
System.out.println(fullName); // Output: John Doe
}
}Output:
John
John Doe
This example shows the immutable nature of strings in Java. Calling concat() on the name string does not change the name. It returns a new String, leaving the name unchanged.
Example 2: Concatenating Multiple Strings
public class StringConcatenationExample {
public static void main(String[] args) {
String firstName = "John ";
String lastName = "Doe";
String fullName = firstName.concat(lastName);
fullName = fullName.concat(" - Java Developer");
System.out.println(fullName);
}
}Output:
John Doe – Java Developer
Here, we combine multiple strings by calling concat() numerous times and chaining the method. No matter how many concatenations we perform, the original strings remain unchanged.
Example 3: Chaining with Spaces and Special Characters
public class StringConcatenationExample {
public static void main(String[] args) {
String start = "Hello";
String mid = "world";
String end = "!";
String message = start.concat(" ").concat(mid).concat(". ").concat(end);
System.out.println(message);
}
}Output:
Hello world. !
This example shows we can combine concat() calls to insert strings with spaces, punctuation, or other special characters.
Example 4: Appending at the Beginning
public class StringConcatenationExample {
public static void main(String[] args) {
String name = "John";
String greeting = "Hello ".concat(name);
System.out.println(greeting);
}
}Output:
Hello John
By putting the append string first, we can use concat() to efficiently prepend a string rather than just append.
Conclusion
The Java String concat() method is a core string manipulation tool that enables concatenating multiple strings together into new String instances. As demonstrated through examples of appending, prepending, and chaining strings with spaces and punctuation, concat() provides an efficient way to build dynamic strings from immutable components. By understanding concat()’s signature, behavior, and use cases, developers gain more control over crafting precise textual output in their programs.
Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

