The Wayback Machine - http://web.archive.org/web/20240204103810/https://www.geeksforgeeks.org/java-string-concat-examples/
Open In App
Related Articles

Java String concat() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The Java String concat() method concatenates one string to the end of another string. It is used for string concatenation in Java.

Examples of Java String concat() Method

The following examples demonstrate how to use the String concat() function in a Java program

java




// Java program to Illustrate Working of concat() method
// with Strings
// By explicitly assigning result
 
// Main class
class GFG {
    // Main driver method
    public static void main(String args[])
    {
        // Custom input string 1
        String s = "Hello ";
 
        // Custom input string 2
        s = s.concat("World");
 
        // Explicitly assigning result by
        // Combining(adding, concatenating strings)
        // using concat() method
 
        // Print and display combined string
        System.out.println(s);
    }
}


Output

Hello World

String concat() Method in Java

Java concat() method returns a string with the value of the string passed into the method, appended to the end of the string.

Syntax of Java String concat()

public String concat (String anostr);

Parameters: A string to be concatenated at the end of the other string
Return Value: Concatenated(combined) string

Below is another Example of String concat() Method:

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.

Using ‘+’ for String Concatenation in Java

We can use the ‘+’ operator for strings in the same way we use for variables.

Below is the implementation of the above topic:

Java




// Java Program to concatenate
// two Strings
import java.io.*;
 
// Driver Class
class GFG {
    // main function
    public static void main(String[] args)
    {
        // str 1
        String str1 = "Hello";
 
        // + operator do concat here!
        String result = str1 + ", World!";
 
        // Output: Hello, World!
        System.out.println(result);
    }
}


Output

Hello, World!

Feeling lost in the vast world of Backend Development? It's time for a change! Join our Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:

  • Comprehensive Course
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world Projects
  • Proven Track Record with 100,000+ Successful Geeks

Last Updated : 25 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials