✕ Close

Week 15 · 15.4 · Thu, Nov 12 · The Specific Skills + Try Its

Join the Strings: Building Text in Java

This section shows the four kinds of steps on today's bench: joining strings with +, working + from left to right, joining strings that are stored in boxes, and counting characters with length().

Joining Strings With +

A string is a row of characters. In code, a string literal is written between double quotation marks, and the quotation marks themselves are not printed. When + stands between two strings, it joins them into one new string, a step called concatenation. The left side comes first and the right side follows it, with nothing added in between.

How to work out what a line with + prints:

  1. Look at the two sides of the first +.
  2. If both sides are numbers, add them. If either side is a string, join them.
  3. Carry the result to the next + and repeat, left to right.

Example 1. Join three strings. The middle string is a single space.

1System.out.println("Mardi" + " " + "Gras");
Show the solution
Mardi Gras

Without the " " in the middle, the two words would run together as MardiGras, because + never adds a space by itself.

Example 2. Join a string and a number. Java turns the number into text, then joins it.

1System.out.println("Room " + 12);
Show the solution
Room 12

The space inside "Room " is part of the string, so it prints between Room and 12.

Left to Right

Java works + from left to right, one + at a time. While both sides are numbers, + adds. As soon as one side is a string, every + after it joins. Parentheses are worked first.

Example 3. Numbers first.

1System.out.println(6 + 4 + " runs");
Show the solution
10 runs

6 + 4 is two numbers, so it adds to 10. Then 10 + " runs" has a string on the right, so it joins.

Example 4. Text first.

1System.out.println("runs: " + 6 + 4);
Show the solution
runs: 64

"runs: " + 6 is already a string, runs: 6. Then the 4 is joined on after it, so the line prints runs: 64, not runs: 10.

Example 5. Parentheses first.

1System.out.println("runs: " + (6 + 4));
Show the solution
runs: 10

The parentheses are worked first, while 6 and 4 are still two numbers, so they add to 10. Then 10 is joined on.

Strings in Boxes

A box declared with String holds a string. When a box name is used with +, Java uses what is stored in the box, not the name itself. A box declared with int holds a whole number, and + joins it to a string the same way.

Example 6. Join a String box, a space and an int box.

1String team = "Saints";
2int score = 31;
3System.out.println(team + " " + score);
Show the solution
Saints 31

team holds Saints and score holds 31. The line joins Saints, a space and 31. If team were written in quotation marks, the line would print the word team instead.

Example 7. Grow a string one piece at a time. Each line stores a longer string back in the same box.

1String msg = "Go";
2msg = msg + " Saints";
3System.out.println(msg);
4msg = msg + " go";
5System.out.println(msg);
Show the solution
Go Saints
Go Saints go
line   msg            Output
1      Go
2      Go Saints
3      Go Saints      Go Saints
4      Go Saints go
5      Go Saints go   Go Saints go

Line 2 joins " Saints" onto what msg holds and stores the result back in msg. Line 4 does the same with " go".

Counting Characters With length()

A string's length() method gives the number of characters in it. Letters, digits, spaces and marks all count. The quotation marks around a string literal do not count, because they are not part of the string.

Example 8. Count the characters of two city names.

1String city = "Lafayette";
2System.out.println(city.length());
3String two = "Baton Rouge";
4System.out.println(two.length());
Show the solution
9
11

Lafayette has 9 letters. Baton Rouge has 10 letters and 1 space, so its length is 11.

Example 9. Count after joining. Go Saints go is 10 letters and 2 spaces.

1String msg = "Go";
2msg = msg + " Saints";
3msg = msg + " go";
4System.out.println(msg.length());
Show the solution
12

The bench catches three common mistakes, and each one changes the answer. The first is forgetting the space, because + never adds one. The second is adding numbers that come after a string, when Java joins them instead. The third is leaving out the spaces when counting characters.

Sources: Oracle, The Java Tutorials: Strings and Primitive Data Types; Oracle, The Java Language Specification, Java SE 21 Edition, ยง15.18; course repository, Bench Basics B18 job data (2026), run with the course JDK.

Try It on your own

Work each one in your notebook first, then check it.

Try It 1. What does System.out.println("Red" + "Beans"); print?

Try It 2. What does System.out.println("Gate " + 7); print?

Try It 3. What does System.out.println(1 + 1 + " cats"); print?

Try It 4. What does System.out.println("cats: " + 1 + 1); print?

Try It 5. Which line prints cats: 2?

Try It 6. day holds Friday and n holds 3. What does System.out.println(day + " " + n); print?

Try It 7. msg holds Go. After the line msg = msg + " Saints"; runs, what does msg hold?

Try It 8. What does System.out.println("Po-boy".length()); print?

Try It 9. a holds Red and b holds Beans. What does System.out.println((a + " " + b).length()); print?