Open In App

String concat() Method in Java with Examples

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Example:

Java




class GFG {
    public static void main(String args[])
    {
        String s = "Geeks ";
        s = s.concat("for Geeks");
        System.out.println(s);
    }
}


Output

Geeks for Geeks

Syntax

public String concat (String s);

Parameters

A string to be concatenated at the end of the other string

Returns

Concatenated(combined) string

Exception

NullPointerException- When either of the string is Null.

Java String concat() Examples

Below is an example that shows how to use the string concat() function in Java

Example 1:

This example shows the working of the concat method in Java.

java




// Java program to Illustrate Working of concat() method
// in strings where we are sequentially
// adding multiple strings as we need
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Custom string 1
        String str1 = "Computer-";
 
        // Custom string 2
        String str2 = "-Science";
 
        // Combining above strings by
        // passing one string as an argument
        String str3 = str1.concat(str2);
 
        // Print and display temporary combined string
        System.out.println(str3);
 
        String str4 = "-Portal";
        String str5 = str3.concat(str4);
        System.out.println(str5);
    }
}


Output

Computer--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.

Example 2:

This code shows the NullPointerException in the concat() method.

Java




// Java program to Illustrate NullPointerException
 
// Main class
public class GFG {
 
    // Main driver method
    public static void main(String args[])
    {
        // Custom string 1
        String str1 = "Computer-";
 
        // Custom string 2
        String str2 = null;
 
        // Combining above strings by
        // passing one string as an argument
        String str3 = str1.concat(str2);
 
        // It will raise NullPointerException
        System.out.println(str3);
    }
}


Output:

Exception in thread "main" java.lang.NullPointerException

String concat() Method – Java Programs

Let’s see some coding problems and solve them with the String concat() method in Java.

1. Combining two strings using the Java concat() method

Java




class GFG {
 
    public static void main(String args[]) {
 
        // Declare a string str1.
        String str1 = "To";
 
        // Declare another string str2.
        String str2 = "gether";
 
        // Concatenate the strings str1 and str2 using the concat() method
        //and store the result back in str1.
        str1 = str1.concat(str2);
 
        // Print the final value of str1
        System.out.println(str1);
    }
}


Output

Together

2. Creating a Reversed String using the Java concat() method

Java




public class ReverseString {
 
    public static void main(String[] args) {
        // Declare original string variable.
        String original = "Geeks";
 
        // Declare another string variable named "reversed"
        //and initialize it with an empty string.
        String reversed = "";
 
        // Iterate through each character in the "original" string
        //from the last index to the first.
        for (int i = original.length() - 1; i >= 0; i--) {
            // Extract the current character at index "i" of the "original" string.
            char currentChar = original.charAt(i);
 
            // Convert the character to a String object
            //using the "Character.toString" method.
            String charAsString = Character.toString(currentChar);
 
            // Concatenate the converted character String
            //to the end of the "reversed" string.
            reversed = reversed.concat(charAsString);
        }
 
        // Print the original and reversed strings.
        System.out.println("Original string: " + original);
        System.out.println("Reversed string: " + reversed);
    }
}


Output

Original string: Geeks
Reversed string: skeeG

References

To know more about more String Methods refer to the article Java String Methods

Whether you are a beginner starting Java programming or an experienced looking to brush up on your Java skills, this tutorial will provide you with a deep understanding of concat function and its uses in Java.

The concat method in Java is a fundamental function for string manipulation. With this guide, you can easily perform string concatenation using the concat function.



Last Updated : 16 Feb, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads