BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

String Concatenation in Java

Last Updated: June 7, 2024 by Chaitanya Singh | Filed Under: java

String concatenation is process of combining multiple strings. In Java, there are multiple ways to do this. In this tutorial, we will see several different approaches to do String concatenation in Java.

1. Using the + Operator

One of the easiest and simplest way to concatenate strings in Java is using the + operator.

public class StringConcatenationPlusOperator {
    public static void main(String[] args) {
        // Using + operator for string concatenation
        String firstName = "Chaitanya";
        String lastName = "Singh";
        String fullName = firstName + " " + lastName;

        // Output: Chaitanya Singh
        System.out.println(fullName);
    }
}

2. Using the concat Method

Another way of doing this is by using concat method of String class. I have covered this method in detail here: String concat method.

public class StringConcatenationConcatExample {
    public static void main(String[] args) {
        // Using concat method
        String str1 = "Beginners";
        String str2 = "Book"
        String str3 = ".com";
        String result = str1.concat(str2).concat(str3);
        System.out.println(result); // Output: BeginnersBook.com
    }
}

3. Using StringBuilder or StringBuffer

StringBuilder (or StringBuffer for thread-safe operations) is more efficient for multiple concatenations. You just need to create an instance of StringBuilder (or StringBuffer) and then we can use append() method to concatenate multiple strings as shown in the following example.

public class StringConcatenationExample2 {
public static void main(String[] args) {
// Using StringBuilder/StringBuffer
String str1 = "Good";
String str2 = " ";
String str3 = "Morning";
String str4 = " ";
String str5 = "Guys";
StringBuilder message = new StringBuilder();
message.append(str1);
message.append(str2);
message.append(str3);
message.append(str4);
message.append(str5);
String text = message.toString();
System.out.println(text); // Output: Good Morning Guys
}
}

4. Using String.join()

We can use String.join() method to concatenate multiple strings with a delimiter. I have covered this method in detail here: String join() method.

public class StringConcatenationJoinExample {
    public static void main(String[] args) {
        // Using String.join()
        String str1 = "Hello";
        String str2 = "World!";
        String greet = String.join(" ", str1, str2);
        System.out.println(greet); // Output: Hello World!
    }
}

5. Using String.format()

There is another method String.format() in String class which can be used for formatted string concatenation. Refer: String format() method.

public class StringConcatenationFormatExample {
    public static void main(String[] args) {
        // Using String.format()
        String firstName = "Chaitanya";
        String lastName = "Singh";
        String fullName = String.format("%s %s", firstName, lastName);
        System.out.println(fullName); // Output: Chaitanya Singh
    }
} 

Performance comparison between these methods

  • + Operator: Simple and readable option for combining multiple strings. However, this is not an efficient way of concatenation when we need to combine multiple strings because it can create many intermediate String objects.
  • String.concat(): This is slightly better than + performance wise, but it is still not ideal when we require multiple concatenations.
  • StringBuilder/StringBuffer: This is the best method for concatenation as it doesn’t create intermediate strings.
  • String.join() and String.format(): These methods are mostly used for convenience and readability purposes.

Top Related Articles:

  1. how to copy one hashmap content to another hashmap
  2. Java Array Declaration and Initialization
  3. Java StringBuilder delete()
  4. Java StringBuffer class With Examples
  5. Java String charAt() Method example

Tags: Java-Strings

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap