w3resource

Java String: concat() Method

concat() method - Concatenates the string with another string

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.

Visual presentation of Java String concat() Method

Java String: concat() Method

Java Platform: Java SE 8 and above.

Syntax concat() method

concat(String str)

Parameters concat() method

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

Return Value concat() method

  • String representing the concatenation of this object's characters with the string argument's characters.

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:

Previous:compareToIgnoreCase Method
Next:contains Method



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-tutorial/string/string_concat.php