Skip to main content

How to Join Strings in Java with the + Operator

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

The quickest way to build text from pieces in Java is the + operator: "Hello, " + name + "!". When one side of + is a String, Java turns the other side into text and joins them, which also explains one classic surprise with numbers.

Syntax​

Syntax
String <text> = <string> + <value> + <string>;
PartWhat it does
<string> + <value>When either side is a String, joins the two as text.
<value>Any value: numbers, booleans and objects are turned into text first.
OrderEvaluated from left to right, like other + operations.

The Java Language Specification, section 15.18.1: "If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time."

The Example​

The example greets a person, joins a name and a year, and prints the same sum twice to show the effect of brackets.

Prerequisites​

  • A JDK, the Java Development Kit. java Main.java runs a single file without compiling it first. This guide uses OpenJDK 25.0.5.1.

Step 1: Write the Code​

Create a file named Main.java:

Main.java, which sets name to Ada and year to 1815, prints Hello, Ada!, prints Ada was born in 1815, then prints "1 + 2 = " + 1 + 2 and "1 + 2 = " + (1 + 2).

Main.java
public class Main {
public static void main(String[] args) {
String name = "Ada";
int year = 1815;

System.out.println("Hello, " + name + "!");
System.out.println(name + " was born in " + year);
System.out.println("1 + 2 = " + 1 + 2);
System.out.println("1 + 2 = " + (1 + 2));
}
}

Step 2: Run It​

Terminal
java Main.java
Output
Hello, Ada!
Ada was born in 1815
1 + 2 = 12
1 + 2 = 3

A zsh terminal where java Main.java prints Hello, Ada!, Ada was born in 1815, 1 + 2 = 12 and 1 + 2 = 3.

The command is the same in zsh, bash and PowerShell.

Why 12 and Not 3​

+ runs from left to right. In "1 + 2 = " + 1 + 2, the first + joins the text and 1 into "1 + 2 = 1"; the second + then sees a String on its left, so it joins 2 as text too. The brackets in "1 + 2 = " + (1 + 2) make Java add the numbers first, then join the result.

Common Mistakes​

MistakeWhat happensDo this instead
Adding numbers inside a concatenation without brackets"Total: " + 1 + 2 gives Total: 12Put the sum in brackets
Joining strings with + inside a long loopEach + builds a new String, which adds upUse a StringBuilder in loops
Comparing joined strings with ==Compares objects, not text, so it can be falseUse .equals(...)

Troubleshooting​

SymptomLikely causeFix
Numbers stuck together, such as 12 instead of 3The sum ran after a stringAdd brackets around the arithmetic
null appears in the textOne of the values is nullCheck the value before joining

FAQ​

Is there a way to join a list of strings with a separator?​

Yes: String.join(", ", "a", "b", "c") returns a, b, c.

What about putting values into a template?​

String.format("%s was born in %d", name, year), or System.out.printf to print directly, fills placeholders instead.

Conclusion​

+ joins strings, and turns numbers and other values into text when a String is involved. It runs left to right, so put arithmetic in brackets when it sits inside a concatenation.

Additional Resources​