The strings in Java are implemented using the string class. This string class offers different methods to work on the coding problems. Each method declared in the string class is used as per the need. We may encounter certain situations, where the requirement might be to join a certain string with another string. For this purpose, the + operator and the concat() method are used in Java.
This article will discuss different examples to gain clarity on the concat() method of Java.
How to Use/Implement String concat() Method in Java?
The string concat() method of Java concatenates the two specified strings. The dot syntax in Java concatenates the first declared string with the second string using the concat() method. This method can join the string from the rear as well as from the front.
The syntax for the concat() method is depicted below.
firstString.concat(secondString)
Let’s learn how this method works practically.
Example 1: Joining the Strings Together
The code below shows that the two strings are declared separately and joined using the concat() method in Java.
//Declare the class in Java
class ConcatenateExample1{
//The main() method of Java
public static void main(String args[]){
//The first part of the string
String st1=" This Example belongs to ";
//Using the concat() method to join the two strings
st1 = st1.concat("Joining Two Strings in Java");
//Print the results
System.out.println(st1);
}
}
In the above code block:
- The class is declared as “ConcatenateExample1”.
- In the main() method of Java, the string object is declared as “st1” which contains the first part of the string.
- In the next step, the “concat()” method joins the two strings together and the result is printed using the println() method of Java.
Output
The output below shows that the two strings are printed in concatenated form using the concat() method.

Example 2: Concatenating More than Two Strings
The code below shows that more than two strings can be joined together using the concat() method in Java.
class ConcatenateExample2{
//The main() method of Java
public static void main(String args[]){
//The first part of the string
String st1=" Java is ";
//Using the concat() method to join multiple strings
st1 = st1.concat("a Programming Language");
String st3 = " that requires";
String st4 = " efforts to learn";
String st5 = "!";
//Print the results
String st0 = st1.concat(st3).concat(st4).concat(st5);
System.out.println(st0);
}
}
In the above code block:
- The class and the main() method in Java are declared.
- There are five strings declared that are st1, st3, st4, st5, and st0 that contain the concatenation result.
- The concat() method combines all the strings and prints the result accordingly.
Output
The output below shows that all the specified strings are printed in concatenated form.

Example 3: Using concat() Method With the println() Method
The concat() method can also be invoked within the println() method, allowing the strings to be concatenated and printed without separately declaring the println() method, as shown below.
//Declare the class in Java
class ConcatenateExample3{
//The main() method of Java
public static void main(String args[]){
//The first part of the string
String st1=" This ";
//Using the concat() method to join multiple strings
String st2= "is ";
String st3 = "code ";
String st4 = "in ";
String st5 = "Java ";
//Print the results
System.out.println(st1.concat(st2).concat(st3).concat(st4).concat(st5));
}
}
In the above code block:
- The strings are declared from st1 to st5.
- The concat() method this time is used inside the println() method of Java.
Output
The output below shows that the declared string in Java is printed in a combined form.

Example 4: Concatenating Special Characters
Special characters like “@”, “#” etc. can also be concatenated using the concat() method of Java. The code below utilizes the concat() method to join multiple strings.
//Declare the class in Java
class ConcatenateExample4{
//The main() method of Java
public static void main(String args[]){
//The first part of the string
String st1=" This ";
//Using the concat() method to join multiple strings
st1 = st1.concat(":");
String st3 = " is ";
String st4 = " @ ";
String st5 = " Example 1 ";
//Print the results
String st0 = st1.concat(st3).concat(st4).concat(st5);
System.out.println(st0);
}
}
In the above code block:
- The class is declared as “ConcatenateExample4”.
- In the main() method of Java, the strings are declared along with the special characters.
- The next step involves joining the strings using the concat() method of Java.
Output

Example 5: Concatenating to the Start of the Declared String
In the code below, the string is joined at the starting point instead of the end.
//Declare the class in Java
class ConcatenateExample5{
//The main() method of Java
public static void main(String args[]){
String s1 = "Java";
//Print the results
System.out.println("This is the code in ".concat(s1));
}
}
In the above code block:
- A single string is declared.
- The println() method takes the concat() method therefore, the first and the last part is printed in the combined form.
Output
In the output below, the combined strings are printed accordingly.

Exception While Using the concat() Method in Java
The code below shows that when a null value is joined with a string, the concat() method throws a NullPointerException in Java.
//Declare the class in Java
class ExceptionCase{
//The main() method of Java
public static void main(String args[]){
String s1 = "Java";
String s2 = null;
//Print the results
System.out.println(s2.concat(s1));
}
}
In the above code block:
- The first string is “java” and the second string is “null”.
- The two strings are combined using the concat() method.
Output
The output shows that the NullPointerException is raised in the execution of code since the second string is declared as “null”.

This concludes the working of the String concat() method in Java.
Conclusion
The String concat() method in Java connects the given strings together. The concat() method uses the dot syntax to join more than one string to a single string. In this article, we have implemented different examples to understand the working of the concat() method of strings in Java.