Table of Contents
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.
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
- Using + operator
- Using String’s concat method
- Using StringBuffer or StringBuilder
+ Operator:
Although java does not support operator overloading but you can concatenate two String using + operator.
1 2 3 4 5 |
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.
1 2 3 4 5 |
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.
1 2 3 4 5 |
String str1= "Hello"; String str2="Java2blog" String result= new StringBuffer(str1).append(str2).toString(); |
It is fastest way to concatenate two String.
Performance Comparison:
- + Operator is not recommended for large String concatenation as it creates lots of temporary object and also some what slow.
- If you have lot of concatenation, StringBuffer or StringBuilder is recommended as it is fastest of all.
Java program for String concatenation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
-------------------- Using + operator -------------------- hellojava2blog -------------------- Using String's conat method -------------------- hellojava2blog -------------------- Using StringBuffer or StringBuilder -------------------- hellojava2blog |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.