GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java String concat() Method with Examples - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java String concat() Method with Examples

Last Updated : 7 Apr, 2026

The concat() method in Java is used to append one string to another and returns a new combined string. It does not modify the original string since strings are immutable.

  • Used to concatenate (join) two strings
  • Returns a new string as the result
  • Throws NullPointerException if the argument is null
Java
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);
    }
}

Output
GeeksforGeeks

Syntax

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:

Example: Combining Two Strings with concat() Method

Java
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);
    }
}

Output
GeeksforGeeks

Example: Sequential Concatenation of Multiple Strings

java
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);
    }
}

Output
Computer-Science-
Computer-Science-Portal

Note: Strings are immutable in Java, so every concatenation creates a new string object instead of modifying the existing one.

Example: Handling NullPointerException in String concat()

Java
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

Example: Reversing a String Using concat() Method

Java
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);
    }
}

Output
Geeks
skeeG
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.