Java String Concatenate example shows how to concatenate strings in Java. The example also shows the difference between concatenation operator (+) vs concat
method vs concatenation using StringBuilder/StringBuffer.
How to concatenate strings in Java?
There are several ways using which you can concatenate String in Java as given below.
1) Concatenate String using the concatenation operator (+)
You can use the string concatenation operator “+” to concatenate two or more Strings as given below.
1 2 3 4 5 6 7 8 9 10 11 |
package com.javacodeexamples.stringexamples; public class StringContainsExample { public static void main(String[] args) { String str = "Hello" + "World"; System.out.println(str); } } |
Output
1 |
HelloWorld |
Concatenation using the + operator also works with Objects and primitive values. If the object is concatenated with the String, it is automatically converted to String using the toString
method of it. Primitive values are also converted to a string representation using the respective wrapper class’s toString
method. It even concatenates null without throwing NullPointerException
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javacodeexamples.stringexamples; import java.util.Date; public class StringContainsExample { public static void main(String[] args) { //concatenate primitive value int i = 10; String str = "Number: " + i; System.out.println(str); //concatenate Object Date date = new Date(); String str1 = "Date: " + date; System.out.println(str1); //concatenate null String str2 = "Null value: " + null; System.out.println(str2); } } |
Output
1 2 3 |
Number: 10 Date: Wed Mar 23 19:54:49 IST 2016 Null value: null |
2) Concatenate String using the concat method of String class
You can use concat
method of String class to concatenate two strings as given below.
1 2 3 4 |
String str1 = "Hello"; String str2 = "World"; System.out.println(str1.concat(str2)); |
Output
1 |
HelloWorld |
3) Concatenate String using the StringBuffer or StringBuilder class
StringBuffer or StringBuilder can be used to concatenate multiple strings using the append
method as given below.
1 2 3 4 |
StringBuffer sb = new StringBuffer("Hello"); sb.append("World"); System.out.println(sb.toString()); |
Output
1 |
HelloWorld |
What is the best way to concatenate strings?
Difference between String concatenation using + operator, the concat method, and StringBuffer
1) Concatenating strings using the + operator looks clean and clearly shows the purpose of the expression than concatenation using the concat
method. Moreover, concatenation using the + operator might use the String objects from the pool, while String concatenation using the concat
method always create a new String object.
2) String concatenation using the + operator is converted to a more efficient code which uses the append
method of the StringBuffer/StringBuilder class by the most modern JVMs as given below.
1 |
String str = a + b + c; |
Will be converted to
1 2 3 4 5 |
StringBuffer sb = new StringBuffer(a); sb.append(b); sb.append(c); String str = sb.toString(); |
3) The concat
method only works with strings. You can not append other data types, such as primitive values using it. While the + operator works with all of them.
4) If the first string is null, invoking the concat
method on it will throw NullPointerException
exception. Concatenating them with the + operator will not throw the exception as given in the below example.
1 2 3 4 |
String a = null; String b = a + "Hello"; System.out.println(b); |
Will print
1 |
nullHello |
If the number of concatenation operations is small, concatenation using the + operator is recommended. On the other hand, if you need to do concatenate operation in a loop, using the append
method of StringBuffer class gives more performance.
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.