w3resource logo


Introduction to Java Programming

Java String: concat() Method

Secondary Nav

public String concat(String str)

The concat() method is used to concatenate a given string to the end of another string.

If the length of the argument string is 0, then this String object is returned.

Otherwise, a String object is returned that represents a character sequence that is the concatenation of the character sequence represented by this String object and the character sequence represented by the argument string.

Java Platform: Java SE 8

Syntax :

concat(String str)

Parameters:

Name Description Type
str the String that is concatenated to the end of this String. String

Return Value :
a string that represents the concatenation of this object's characters followed by the string argument's characters.
greater than, equal to, or less than this String, ignoring case
considerations.

Return Value Type: string

Example : Java String concat() Method

public class Example {

public static void main(String[] args)
    {
        String str1 = "PHP Exercises and ";
        String str2 = "Python Exercises";
System.out.println();
System.out.println("String 1: " + str1);
System.out.println("String 2: " + str2); 


        // Concatenate the two strings together.
        String str3 = str1.concat(str2);

        // Display the new String.
System.out.println("The concatenated string: " + str3);
System.out.println();
    }
}

Output:

String 1: PHP Exercises and                            
String 2: Python Exercises                             
The concatenated string: PHP Exercises and Python Exercises

Java Code Editor:



Join our Question Answer community to learn and share your programming knowledge.