Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
In this tutorial, we will learn about different ways to concatenate strings in Java. String concatenation is the process of joining two or more strings together to form a new string.
Example:
public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; String result = str1 + " " + str2; System.out.println(result); } }
Output:
Hello World
Example:
public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; String result = str1.concat(" ").concat(str2); System.out.println(result); } }
Output:
Hello World
Example:
public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; StringBuilder sb = new StringBuilder(); sb.append(str1); sb.append(" "); sb.append(str2); String result = sb.toString(); System.out.println(result); } }
Output:
Hello World
Example:
public class Main { public static void main(String[] args) { String str1 = "Hello"; String str2 = "World"; String result = String.format("%s %s", str1, str2); System.out.println(result); } }
Output:
Hello World
In conclusion, there are various ways to concatenate strings in Java, and the choice depends on the specific use case and requirements. For simple concatenations, the '+' operator or 'concat()' method is suitable, while for more complex or performance-sensitive scenarios, 'StringBuilder' is recommended.
Concatenating strings in Java:
Java allows you to concatenate strings using the +
operator. It's a simple and straightforward way to combine two strings.
String str1 = "Hello"; String str2 = "World"; String result = str1 + " " + str2; // Concatenation using '+' System.out.println(result); // Output: Hello World
Using the '+' operator for string concatenation in Java:
The +
operator can be used to concatenate strings and even mix them with other data types.
String name = "John"; int age = 25; String message = "My name is " + name + " and I am " + age + " years old."; System.out.println(message);
StringBuilder vs String concatenation in Java:
StringBuilder
is more efficient than simple string concatenation, especially in loops or when dealing with many string operations.
StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(" "); sb.append("World"); String result = sb.toString(); System.out.println(result);
Concatenating strings with Java 8+ features:
Java 8 introduced the StringJoiner
class and the String.join()
method, providing more concise ways to concatenate strings.
StringJoiner sj = new StringJoiner(", "); sj.add("Apple").add("Orange").add("Banana"); String result = sj.toString(); System.out.println(result);
Concatenation of strings with variables in Java:
Variables can be easily incorporated into concatenated strings using the +
operator.
String name = "Alice"; int age = 30; String message = "Name: " + name + ", Age: " + age; System.out.println(message);
StringBuffer for thread-safe string concatenation in Java:
If thread safety is a concern, StringBuffer
can be used as it provides synchronized methods for string concatenation.
StringBuffer buffer = new StringBuffer(); buffer.append("Thread").append("Safe"); String result = buffer.toString(); System.out.println(result);
Concatenating strings in a loop in Java:
When concatenating strings in a loop, it's advisable to use StringBuilder
for better performance.
StringBuilder sb = new StringBuilder(); for (int i = 1; i <= 5; i++) { sb.append("Number ").append(i).append(" "); } String result = sb.toString(); System.out.println(result);
Concatenating strings with special characters in Java: Special characters can be included in concatenated strings as needed.
String specialChar = "!"; String greeting = "Hello" + specialChar; System.out.println(greeting);
Java String concatenation with whitespace removal:
If there is a need to remove whitespace during concatenation, you can use trim()
.
String str1 = " Hello"; String str2 = "World "; String result = str1.trim() + " " + str2.trim(); System.out.println(result);