All lessons

Working with Text

7 min · Java

Join strings, measure them, and format text with String tools.

Java · Working with Text0/5 cleared

Enemy 1 of 5

This code joins two pieces of text with + and prints the result. Press Run to see the greeting and name glued together.

Why this works

Text is stored in String objects. The word "objects" matters here: unlike int or double, String is a full-fledged class in Java, which is why it comes with built-in methods (shown below) rather than just being a raw value.

Join two strings with + (concatenation), the same operator used for addition, but Java uses it for gluing text together whenever at least one side is a String. This double duty is convenient, and it also means you can mix a String and a number with + (like "Score: " + 10) and Java will automatically convert the number to text for you, a small but handy difference from stricter languages.

You
Packet Wolf
Loading editor…
Sign up to fightThis one runs on our servers, so it needs a free account.