Java String concat() Method with Examples
Last Updated :
11 Jul, 2025
The string concat() method concatenates (appends) a string to the end of another string. It returns the combined string. It is used for string concatenation in Java. It returns NullPointerException if any one of the strings is Null.
In this article, we will learn how to concatenate two strings in Java.
Program to Concatenate Two Strings using concat() Method in Java
Below is the simplest method to concatenate the string in Java. We will be using the concat() method of String class to append one string to the end of another and return the new string.
Java
// Java Program to Illustrate concat() method of String.
class GFG {
public static void main(String args[]) {
// String Initialization
String s = "Geeks";
// Use concat() method for string concatenation
s = s.concat("forGeeks");
System.out.println(s);
}
}
Syntax of concat() Method
public String concat (String s);
Parameters:
- A string to be concatenated at the end of the other string.
Return Value:
- Concatenated(combined) string.
Exception:
- NullPointerException: When either of the string is Null.
Java String concat() Examples
There are many ways to use the concat() method in Java:
Sequential Concatenation of Multiple Strings
The below example shows sequential concatenation using String concat() method in Java.
Example:
java
// Java program to Illustrate Working of concat() method
// in strings where we are sequentially
// adding multiple strings as we need
public class GFG {
public static void main(String args[])
{
String s1 = "Computer-";
String s2 = "Science-";
// Combining above strings by
// passing one string as an argument
String s3 = s1.concat(s2);
// Print and display temporary combined string
System.out.println(s3);
String s4 = "Portal";
String s5 = s3.concat(s4);
System.out.println(s5);
}
}
OutputComputer-Science-
Computer-Science-Portal
Note: As perceived from the code we can do as many times as we want to concatenate strings bypassing older strings with new strings to be contaminated as a parameter and storing the resultant string in String datatype.
Handling NullPointerException
in String concat()
The below example shows the NullPointerException in String concat() method.
Example:
Java
// Java program to Illustrate NullPointerException
public class GFG {
public static void main(String args[])
{
String s1 = "Computer-";
String s2 = null;
// Combining above strings by
// passing one string as an argument
String s3 = s1.concat(s2);
// It will raise NullPointerException
System.out.println(s3);
}
}
Output:
Exception in thread "main" java.lang.NullPointerException
Combining Two Strings with concat() Method
The below example combines two strings using concat() method of string class.
Example:
Java
// Java Program to combine two strings with concat()
class GFG {
public static void main(String args[]) {
// String Initialization
String s1 = "Geeksfor";
String s2 = "Geeks";
// Concatenate the strings s1 and s2 using the concat() method
// and store the result back in s1.
s1 = s1.concat(s2);
System.out.println(s1);
}
}
Reversing a String Using concat()
Method
We can reverse a string using the concat() method of string class. Below is the example to reverse a string in Java.
Example:
Java
// Java Program to reverse a string using concat() method
public class ReverseString {
public static void main(String[] args) {
// Declare original string variable
String a = "Geeks";
// Declare another string variable
// and initialize it with an empty string
String b = "";
// Iterate through each character in string "a"
// from the last index to the first.
for (int i = a.length() - 1; i >= 0; i--) {
// Extract the current character at index "i" of the "a" string
char ch = a.charAt(i);
// Convert the character to a String object
// using the "Character.toString" method
String ch1 = Character.toString(ch);
// Concatenate the converted character String
// to the end of the "b" string
b = b.concat(ch1);
}
System.out.println("" + a);
System.out.println("" + b);
}
}
Java Program to Concatenate Two Strings
Similar Reads
Constructor toString() method in Java with Examples The toString() method of java.lang.reflect.Constructor class is used to return string representation of this Constructor.This string is the collection of all the properties of this constructor. For creating this string method adds access modifiers of the constructor with the name of the declaring cl
2 min read
Java Guava | Shorts.concat() method with Examples Shorts.concat() is a method of Shorts class in Guava library which is used to concatenate the values of multiple arrays into a single array. For example, concat(new short[] {a, b}, new short[] {}, new short[] {c} returns the array {a, b, c}. Syntax : public static short[] concat(short[]... arrays) H
2 min read
CompoundName toString() method in Java with Examples The toString() method of a javax.naming.CompoundName class is used to create the string representation of this compound name object, using the syntax of the compound name which was passed through properties. if the component is empty then it is represented by an empty string. The string representati
2 min read
CompositeName toString() method in Java with Examples The toString() method of a javax.naming.CompositeName class is used to create the string representation of this composite name object, using the syntax "/" as a separator of each object. The string representation returned by this composite name consists of enumerating in order each component of this
2 min read
CompositeName toString() method in Java with Examples The toString() method of a javax.naming.CompositeName class is used to create the string representation of this composite name object, using the syntax "/" as a separator of each object. The string representation returned by this composite name consists of enumerating in order each component of this
2 min read
Java String join() with examples The java.lang.string.join() method concatenates the given elements with the delimiter and returns the concatenated string.Note that if an element is null, then null is added.The join() method is included in java string since JDK 1.8. There are two types of join() methods in java string. :public stat
2 min read