Concatenation in the Java programming language is the operation of joining two strings together. You can join strings using either the addition (+) operator or the String’s concat() method.
Using the + Operator
Using the + operator is the most common way to concatenate two strings in Java. You can provide either a variable, a number, or a String literal (which is always surrounded by double quotes).
To combine the strings “I’m a” and “student”, for example, write:
"I’m a" + " student"
Be sure to add a space so that when the combined string is printed, its words are separated properly. Note above that " student" starts with a space, for example.
Combining Multiple Strings
Any number of + operands can be strung together, for instance:
"I’m a" + " student" + "! And so are you."
Using the + Operator in a Print Statement
Frequently, the + operator is used in a print statement. You might write something like:
System.out.println("pan" + "handle");
This would print:
panhandle
Combining Strings Across Multiple Lines
Java disallows literal strings to span more than a line. Using the + operator prevents this:
String quote =
"Nothing in all the world is more dangerous than " +
"sincere ignorance and conscientious stupidity.";
Combining a Mixture of Objects
The operator "+" normally acts as an arithmetic operator unless one of its operands is a String. If so, it converts the other operand to a String before joining the second operand to the end of the first operand.
For example, in the example below, age is an integer, so the + operator will first convert it to a String and then combine the two strings. (The operator does this behind the scenes by calling its toString() method; you won’t see this occur.)
int age = 12;
System.out.println("My age is " + age);
This would print:
My age is 12
Using the Concat Method
The String class has a method concat() that performs the same operation. This method acts on the first string and then takes the string to combine as a parameter:
public String concat (String str)
For example:
String myString = " I have decided to stick with love.;
myString = myString.concat(" Hate is too great a burden to bear.");
System.out.println(myString);
This would print:
I have decided to stick with love. Hate is too great a burden to bear.
Differences Between the + Operator and the Concat Method
You may be wondering when it makes sense to use the + operator to concatenate, and when you should use the concat() method. Here are some differences between the two:
- The concat() method can combine only String objects — it must be called on a String object, and its parameter must be a String object. This makes it more restrictive than the + operator since the operator silently converts any non-string argument to a string.
- The concat() method throws a NullPointerException if the object has a null reference, while the + operator deals with a null reference as a “null” string.
- The concat()) method is capable of combining only two strings – it cannot take multiple arguments. The + operator can combine any number of strings.
For these reasons, the + operator is more often used to combine strings. If you are developing a large-scale application, however, performance can differ between the two because of the way that Java handles string conversion, so be aware of the context in which you are combining strings.