Java String Concatenation
Combining strings together in Java
🔗 What is String Concatenation?
String concatenation is the process of joining two or more strings together to create a single string. Java provides multiple ways to combine strings efficiently.
// Basic concatenation with + operator
String firstName = "John";
String lastName = "Doe";
String fullName = firstName + " " + lastName;
System.out.println("Full name: " + fullName);
System.out.println("Welcome, " + firstName + "!");
Output:
Full name: John Doe
Welcome, John!
Concatenation Methods
Plus Operator
Simple and most common method
String result = "Hello" + " " + "World";
String greeting = "Hi " + name;
concat() Method
Built-in string method
String result = "Hello".concat(" World");
String full = first.concat(" ").concat(last);
StringBuilder
Efficient for multiple concatenations
StringBuilder sb = new StringBuilder();
sb.append("Hello").append(" World");
String result = sb.toString();
String.format()
Template-based concatenation
String result = String.format("%s %s",
firstName, lastName);
🔹 Using the + Operator
The most common and simple way to concatenate strings:
// Basic concatenation
String greeting = "Hello";
String name = "Alice";
String message = greeting + ", " + name + "!";
// Concatenating with numbers
int age = 25;
double salary = 50000.50;
String info = name + " is " + age + " years old and earns $" + salary;
// Multiple concatenations
String address = "123" + " " + "Main" + " " + "Street";
// Concatenating with boolean
boolean isActive = true;
String status = "User is " + (isActive ? "active" : "inactive");
System.out.println(message);
System.out.println(info);
System.out.println(address);
System.out.println(status);
Output:
Hello, Alice!
Alice is 25 years old and earns $50000.5
123 Main Street
User is active
🔹 Using concat() Method
The concat() method joins strings together:
String first = "Java";
String second = "Programming";
// Using concat() method
String combined = first.concat(" ").concat(second);
// Chaining multiple concat() calls
String sentence = "I"
.concat(" love")
.concat(" Java")
.concat(" programming!");
// concat() with variables
String prefix = "Mr. ";
String name = "Smith";
String title = prefix.concat(name);
System.out.println("Combined: " + combined);
System.out.println("Sentence: " + sentence);
System.out.println("Title: " + title);
Output:
Combined: Java Programming
Sentence: I love Java programming!
Title: Mr. Smith
🔹 Using StringBuilder
StringBuilder is efficient for multiple concatenations:
// Creating StringBuilder
StringBuilder builder = new StringBuilder();
// Adding strings with append()
builder.append("Hello");
builder.append(" ");
builder.append("World");
builder.append("!");
// Method chaining
StringBuilder chain = new StringBuilder()
.append("Java")
.append(" is")
.append(" awesome");
// Building a sentence with loop
StringBuilder sentence = new StringBuilder("Numbers: ");
for (int i = 1; i <= 5; i++) {
sentence.append(i).append(" ");
}
// Convert to String
String result1 = builder.toString();
String result2 = chain.toString();
String result3 = sentence.toString();
System.out.println("Result 1: " + result1);
System.out.println("Result 2: " + result2);
System.out.println("Result 3: " + result3);
Output:
Result 1: Hello World!
Result 2: Java is awesome
Result 3: Numbers: 1 2 3 4 5
🔹 Using String.format()
Template-based string formatting and concatenation:
String name = "Bob";
int age = 30;
double height = 5.9;
boolean isMarried = true;
// Using format specifiers
String info = String.format("Name: %s, Age: %d, Height: %.1f ft",
name, age, height);
// Different format specifiers
String details = String.format(
"Person: %s\nAge: %d years\nHeight: %.2f feet\nMarried: %b",
name, age, height, isMarried);
// Formatting numbers
double price = 1234.567;
String formatted = String.format("Price: $%.2f", price);
// Date formatting
String date = String.format("Today is %02d/%02d/%d", 12, 25, 2023);
System.out.println(info);
System.out.println(details);
System.out.println(formatted);
System.out.println(date);
Output:
Name: Bob, Age: 30, Height: 5.9 ft
Person: Bob
Age: 30 years
Height: 5.90 feet
Married: true
Price: $1234.57
Today is 12/25/2023
🔹 Performance Comparison
Choose the right method based on your needs:
When to use each method:
- + Operator: Simple concatenations, few strings
- concat(): Joining exactly two strings
- StringBuilder: Many concatenations, loops, performance critical
- String.format(): Complex formatting, templates
// Example: Building a report
StringBuilder report = new StringBuilder();
report.append("=== SALES REPORT ===\n");
String[] products = {"Laptop", "Mouse", "Keyboard"};
double[] prices = {999.99, 25.50, 75.00};
for (int i = 0; i < products.length; i++) {
String line = String.format("%s: $%.2f\n", products[i], prices[i]);
report.append(line);
}
report.append("===================");
System.out.println(report.toString());
Output:
=== SALES REPORT ===
Laptop: $999.99
Mouse: $25.50
Keyboard: $75.00
===================