×

How to Concatenate Two Strings in Java

How to Concatenate Two Strings in Java

Concatenation of two strings means adding the beginning of one string to the end of the other string. Some of the ways to concatenate two strings in Java are mentioned below.

By Using + Operator

The + operator can be used to concatenate the strings. Let’s understand it with the help of the following example.

FileName: StringConcatenationExample.java

 public class StringConcatenationExample
{ 
// main method
public static void main(String argvs[])
{
// Input string
String str1 = "Tutorial & Example";
// Doing the concatenation work
str1 = str1 + " – A Good Site To Learn Java.";
System.out.println(str1);
}
} 

Output:

Tutorial & Example – A Good Site To Learn Java.


Explanation: By looking at the output, it is evident that the + operator concatenates the strings “Tutorial & Example” and “– A Good Site To Learn Java.”

By Using the concat() Method

The concat() method can also be used to do the concatenation work.

Syntax:

public String concat (String  anotherStr)

String anotherStr: represents the string that is being concatenated with the current string.

A String is returned, which is the concatenation of the current string and anotherStr.

The following program illustrates the same.

FileName: StringConcatenationExample1.java

 public class StringConcatenationExample1
{ 
// main method
public static void main(String argvs[])
{
// Input string
String str1 = "Tutorial & Example";
// Doing the concatenation work
str1 = str1.concat(" – A Good Site To Learn Java.");
System.out.println(str1);
}
} 

Output:

Tutorial & Example – A Good Site To Learn Java.


Explanation: In the above program, the + operator is replaced by the concat() method to accomplish the concatenation work. Everything else remains the same. Hence, the output also remains the same.

By Using the append() Method

Another approach is to use the append() method to do the concatenation of the given strings. However, the append() method is not present in the Java String class. Therefore, to apply the append() method, one needs the object of either StringBuilder or StringBuffer class. Observe the following program.

FileName: StringConcatenationExample2.java

 public class StringConcatenationExample2
{ 
// main method
public static void main(String argvs[])
{
// Input string
String str1 = "Tutorial & Example";
// Creating an object of the StringBuilder class
StringBuilder strBldrObj = new StringBuilder(str1);
// Doing the concatenation work
strBldrObj = strBldrObj.append(" – A Good Site To Learn Java.");
// getting the string from the
// object of the StringBuilder class
str1 = strBldrObj.toString();
// displaying the string
System.out.println(str1);
str1 = "Let's visit";
// Creating an object of the StringBuffer class
StringBuffer strBfrObj = new StringBuffer(str1);
// Doing the concatenation work
strBfrObj = strBfrObj.append(" The Tutorial & Example Site Again To Learn Java.");
// getting the string from the
// object of the StringBuffer class
str1 = strBfrObj.toString();
// displaying the string
System.out.println(str1);
}
} 

Output:

 Tutorial & Example – A Good Site To Learn Java.
Let's visit The Tutorial & Example Site Again To Learn Java. 


Explanation: The above program shows that either StringBuilder or StringBuffer class can be used to do the concatenation of string. The concatenation of strings is achieved by invoking the append() method on the objects of the classes StringBuilder or StringBuffer. The toString() method then used to extract the concatenated string from those objects, which is displayed in the output.


Related Topics

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Types of Bitwise Operators in Java

In this tutorial, we will learn about the various types or kinds of bitwise operators in java. Before proceeding to bitwise operators, let us know what is meant by the...

6 minutes read.

Java Clone Array

We frequently need to copy an array to back up its original components. We have a few unique numbers and strings, including Armstrong numbers, palindrome numbers, and palindrome strings. To...

4 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

How Many Ways to Create an Object in Java?

As you know, a class is a blueprint for an object, and you can create objects from it. There are several ways to create objects of classes in Java. This...

6 minutes read.

Java Buffered Writer

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

Root exception in java

Java: The main feature of java which is not in C or object oriented programming language is platform independence. Not only the platform independence there are many other features in java...

3 minutes read.

Java String getBytes() method

getBytes() method returns sequence of bytes. Syntax: There are three variants of getBytes() method: public byte[] getBytes()        public byte[] getBytes(String charsetName) public byte[] getBytes(Charset charset) Returns: It returns sequence of bytes. Java String getBytes() Example...

2 minutes read.

Java Boolean booleanValue() method

The booleanValue() method of Java Boolean class returns a Boolean value for the specified Boolean argument. Syntax public Boolean booleanValue() Parameters NA Return Value This method returns the primitive value of specified Boolean object. Example 1 public class...

2 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Java RMI

What is RMI in Java? A framework for developing distributed Java applications is provided by the RMI (Remote Method Invocation) API. An object can call methods on an object running in...

4 minutes read.

What is string in Java why it's immutable

String in Java Strings are a series of characters commonly used in Java language, which are considered objects in the Java. To create and manipulate strings, java will provide the String...

4 minutes read.

Abstract Class Program in Java

Abstract Class Program in Java Abstraction is a technique by which a developer hides the implementation details from the user and shows only the functionality.It is not only confined to the...

6 minutes read.

How to use Lambda Expression in Java?

The new and significant lambda expression feature of Java was added in Java SE 8. It provides a clear and concise mechanism for describing a single method interface using an...

4 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.

Java Transient

Java Transient In Java, Serialization is used to convert an object into a stream of the byte. The byte stream consists of the data of the instance as well as the...

3 minutes read.