Java String concatenate example

String concatenate is a way to join multiple Strings to create large String. There are many places where we use to do String concatenate.
For example: When you override toString() method of object, you concatenate multiple attributes to create a big String that represent that object.

3 ways to concatenate String

  1. Using + operator
  2. Using String’s concat method
  3. Using StringBuffer or StringBuilder

+ Operator:

Although java does not support operator overloading but you can concatenate two String using + operator.

String str1= "Hello";
String str2="Java2blog"
String result= str1+str2;

Here result will be “HelloJava2blog” .

String’s concat method:

You can use String ‘s concat method to concatenate two String.

String str1= "Hello";
String str2="Java2blog"
String result= str1.concat(str2);

Here result will be “HelloJava2blog” .

StringBuilder or StringBuffer:

You can use StringBuilder or StringBuffer’s append method to concatenate two String.

String str1= "Hello";
String str2="Java2blog"
String result= new StringBuffer(str1).append(str2).toString();

It is fastest way to concatenate two String.

Performance Comparison:

  1. + Operator is not recommended for large String concatenation as it creates lots of temporary object and also some what slow.
  2. If you have lot of concatenation, StringBuffer or StringBuilder is recommended as it is fastest of all.

Java program for String concatenation:

package org.arpit.java2blog;

public class StringConcatenateExample {
 /*
  * @ Author : Arpit Mandliya
  */
 public static void main(String args[]) {

  String str1="hello";
  String str2="java2blog";
  // Using String's constructor
  System.out.println("--------------------");
  System.out.println("Using + operator");
  System.out.println("--------------------");

  // Below statement internally calls:
  // new StringBuffer().append(str1).append(str2)
  String resultUsingPlus=str1+str2;
  System.out.println(resultUsingPlus);
  System.out.println();

  // Using String's valueOf method
  System.out.println("--------------------");
  System.out.println("Using String's conat method");
  System.out.println("--------------------");
  String resultUsingConcat=str1.concat(str2);
  System.out.println(resultUsingConcat);
  System.out.println();

  // Using iterating char Array
  System.out.println("--------------------");
  System.out.println("Using StringBuffer or StringBuilder");
  System.out.println("--------------------");
  String resultUsingbuilder=new StringBuffer(str1).append(str2).toString();
  System.out.println(resultUsingbuilder);
 }
}

When you run above program, you will get below output

--------------------
Using + operator
--------------------
hellojava2blog

--------------------
Using String's conat method
--------------------
hellojava2blog

--------------------
Using StringBuffer or StringBuilder
--------------------
hellojava2blog
[social_share share_on="facebook,twitter,linkedin,whatsapp"][post_navigator in_same_term="true"]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.