Udemy logo

java string concatenationThe Java String Class is the prime class in Java that is used to handle strings of data in the application. To use the String class in an application, there isn’t any need to import any specific package since String class resides in java.lang package which is automatically accessible to every class in Java. The String class contains a variety of handy functions that can be used to manage and manipulate String type objects. String concatenation is one such functionality that can be achieved using built-in String Class methods.

To learn essential Java concepts, take a course at Udemy.com.

What is Java String Concatenation?

String concatenation refers to joining the character sequence contained by two strings and returning a new string that contains that joined sequence of characters. The object of the String class is immutable which means that it cannot be changed, altered or modified. Whenever a string object is altered, a new string object is created in which the altered string is stored. It is for this reason that string concatenation results in a new string object that contains concatenated or joined sequence of characters from the two strings. A mutable version of strings also exists which is called StringBuilder class.

How Java string Concatenation is performed?

In Java, there are two ways to concatenate two strings. You can use the concat method or the plus “+” character.

The concat method is an instance method of String class. This method takes a String type object as an argument and returns concatenated String type object. The syntax of the concat method in String class looks like this:

public String concat (String str)

It is evident from the above method definition that the return type of concat is String object and the only parameter it takes is also a String type object. The following example demonstrates how the concat method is used in Java to concatenate String type objects.

String str = “First String “;

String str2 = “Second String”;

String str3 = str.concat (str2);

 

System.out.println(str3);

In the above example, two String object: str and str2 have been instantiated using string literals. The object str contains the literal string “First String” and the str2 object contains “Second String”. The method concat is then called on the String object str and the parameter String object is str.

The concat method performs concatenation of str and str2 and the returned object would be stored in another String object str3. The str3 is then displayed on the console. The str3 contains string “First String Second String”, i.e. the concatenation of str and str2.

Apart from taking a String object as parameter, the concat method can also use a string literal as a parameter and would append the literal value passed to it at the end of the String object on which the concat method has been called. Also, the concat method can directly be called on a string literal.

The following example demonstrates this concept:

String str = “First String “.concat(“Second String”);

System.out.println(str);

In the above example, the concat method is called on the string literal “First String ” and the parameter passed to it is also a string literal “Second String”. The concat method performs the concatenation of these two string literals, creates an underlying string object that contains the concatenation of string literals, and returns the object to String str. The String str printed on the console is “First String Second String”.

Apart from using the concat method, an addition operator can also be used to perform concatenation between two or more String type objects. This concept is explained in the following example:

String str = “First String “;

String str2 = “Second String”;

String str3 = str + str2;

System.out.println(str3);

The above example is similar to one in which concatenation using the concat method was demonstrated. The only difference is that the str and str2 String objects are concatenated via addition “+” operator.

The “+” operator can be used to concatenate as many strings as desired in a single line of code. The following example demonstrates this concept:

String str = “First String “;

String str2 = “Second String”;

String str3 = str + str2 + ” Third String” + ” Fourth String”;

System.out.println(str3);

In the above code, apart from concatenating two string objects str and str2, two string literals “ Third String” and “ Fourth String” also been concatenated.  The str3 string would contain string “First String Second String Third String Fourth String”.

Another interesting thing about “+” operator is that it can also be used to concatenate String objects with other types of objects. This is explained in the following example:

String str = “First String “;

int num =5;

String str2 = str + num;

System.out.println(str2);

When the types other than String are concatenated with String type objects as in the above case, there toString() method is called to covert the object into string type.

Want to explore more String functions? Take a course at Udemy.com.

Difference between Concat Method and “+” Operator

Though both, the concat method and addition operator look same in their functionality; both perform concatenation on strings. However, there are some differences between the two which are as follows:

(To study Java in detail, start from this course)

 

Page Last Updated: May 2014

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo