Input: “Hello” and “World”
Output: “HelloWorld”
Input: “1234” and “5678”
Output: “12345678”
Read Also : 5 Ways to Reverse a String in Java with Example
Points to Keep in Mind Before attempting the Solution :
1. String is immutable so once it is created in memory we will not be able to modify it rather than assigning new String. StringBuffer and StringBuilder are mutable, so once it is created , modifications can be done later.
2. The first argument of String.join() method is delimiter used to join strings and there can any number of string arguments starting from second argument.
Each way has specific advantage and disadvantage, so use it according to your requirements.
Pseudo Code for Concatenating String Method 1 (Using + operator):
1. Take two String that need to be concatenated.
2. Concatenate both String and store into String object.
3. Print the result of concatenated Strings on console.
import java.lang.*; public class concatenateString { public static void main(String[] args) { // First String as an Object String str = "Hello"; // Concatenating and storing above String object and constant string String result = str + "World"; // Print the result System.out.println(result); }}
Best Use: When concatenating the constant strings.
Worst Use: When using + operator inside loop.
Pseudo Code for Concatenating String Method 2 (Using String.concat() method):
1. Take two String that need to be concatenated.
2. Concatenate first String with Second string using concat() method and store into String object.
3. Print the result of concatenated Strings on console.
import java.lang.*; public class concatenateString { public static void main(String[] args) { // Two String objects String str1 = "Hello"; String str2 = "World"; /* Using concat() method, concat first string with second string and
storing into result string */
String result = str1.concat(str2); // Print the result System.out.println(result); }}
Best Use: When concatenating string objects.
Worst Use: When using on dynamic objects, if str1 becomes null before concat() method it will throw NullPointerException.
Pseudo Code for Concatenating String Method 3 (Using StringBuffer):
1. Take two String that needs to be concatenated.
2. Append both String objects into StringBuffer object.
3. Print the StringBuffer object.
import java.lang.*; public class concatenateString { public static void main(String[] args) { // Two String objects String str1 = "Hello";
String str2 = "World";
// StringBuffer object
StringBuffer sb = new StringBuffer();
// Appending str1 and str2
sb.append(str1).append(str2);
// Print the result System.out.println(sb); }}
Best Use: When concatenating large number of string objects in multithreaded application.
Worst Use: When creating StringBuffer object inside loop.
Pseudo Code for Concatenating String Method 4 (Using StringBuilder):
1. Take two String that needs to be concatenated.
2. Append both String objects into StringBuilder object.
3. Print the StringBuilder object.
import java.lang.*; public class concatenateString { public static void main(String[] args) { // Two String objects String str1 = "Hello";
String str2 = "World";
// StringBuilder object
StringBuilder sb = new StringBuilder();
// Appending str1 and str2
sb.append(str1).append(str2);
// Print the result System.out.println(sb); }}
Best Use: When concatenating large number of string objects in single threaded application.
Worst Use: When creating StringBuilder object inside loop or using in multithreaded application can lead unexpected result.
Pseudo Code for Concatenating String Method 5 (Using String.join() method of Java 8):
1. Take two String that needs to be concatenated.
2. Concatenate and store above Strings using String.join() method.
3. Print the result String.
import java.lang.*; public class concatenateString { public static void main(String[] args) { // Two String objects String str1 = "Hello";
String str2 = "World";
// Concatenation and storing str1 and str2 object
StringBuffer result = String.join("", str1, str2);
// Print the result System.out.println(result); }}
Best Use: When concatenating strings with delimiter or an array of strings.
Worst Use: When concatenating large number of Strings.
Please mention in the comments in case if you know any way for string concatenation in java other then the above mentioned ways.