concat() String Example

java.lang.String class comes with concat() String method to concatenate two strings.

The signature is given as defined in String class.

  • public String concat(String str): Returns a new string, the result of concatenation of two strings.

Following example concat() String illustrates the usage concat() method.

public class StringMethodDemo
 public static void main(String args[])
  String str1 = "Hello";
  String str2 = "World";

  String str3 = str1.concat(str2);

  System.out.println("Concatenation of " + str1 + " and " + str2 + " is " + str3);
  System.out.println("Concatenation of Raja and Rani is " + "Raja".concat("Rani"));
  System.out.println("\nUsing + operator: " + "Raja"+"Rani");

concat() String
Output screenshot on concat() String Example

By performance concat() is nearly 40 times faster than using + operator.

Pass your comments and suggestions on this tutorial "concat() String Example".

Leave a Comment

Your email address will not be published.