Joining strings with +
Concatenation means gluing strings end to end. In Scala the + operator does it when its left side is a string:
val message = "I love coding. " + "It is fun!"
println(message)
I love coding. It is fun!
With two literals this is pointless, since you could write the long string directly. Concatenation earns its place when some parts are variables, so the message changes with the data:
val animal = "parrot"
val name = "Andy"
println(name + ", that's a nice " + animal + " you got there!")
Andy, that's a nice parrot you got there!
Change animal to "dog" and the same line prints a sentence about a dog.
The exercise step by step
The starting code defines two values and prints nothing:
val name: String = "John Doe"
val language: String = "Python"
The task is to print My name is John Doe and I code in Python by building the sentence from these values.
- Start with the fixed text. The sentence begins with
"My name is ", including the space before the closing quote. - Add the first variable. Put
+ nameafter it, without quotes, so Scala uses the variable's value instead of the word "name". - Add the next piece of text.
" and I code in "needs a space on both sides, because nothing else will supply them. - Finish with
language. Add+ language, again without quotes. - Wrap it in
println. The whole expression goes inside the parentheses, so the finished sentence is printed.
The solution in Scala
val name: String = "John Doe"
val language: String = "Python"
println("My name is " + name + " and I code in " + language)
It prints:
My name is John Doe and I code in Python
Scala evaluates the + operators from left to right, building one longer string at each step.
Details worth knowing
- Quotes decide text or variable.
"name"is the word name.nameis the variable's value. - Spaces are your job. If the second piece of text were
"and I code in "without the leading space, the output would readJohn Doeand I code in Python. - Numbers can be joined too. When the left side is a string,
+converts a number on the right to text:"Age " + 32gives"Age 32". - Order matters with numbers. Scala works from left to right, so
1 + 2 + " items"adds first and gives3 items, while"items " + 1 + 2givesitems 12. - Strings are immutable.
+never changes the original strings. It produces a new one.
The s interpolator
Long chains of + are easy to get wrong. Scala offers another way: put an s before the opening quote and write $name inside the text.
println(s"My name is $name and I code in $language")
It prints the same sentence. For an expression rather than a single name, use braces: s"next year: ${age + 1}". Without the s, the text $name is printed literally. This lesson asks for +, so use that.
Common mistakes
- Putting a variable name inside the quotes, so the output contains the word instead of the value.
- Forgetting a space between pieces, which glues two words together.
- Leaving out a
+between two parts, which doesn't compile. - Putting a number first in the chain and expecting text:
1 + 2 + " items"is arithmetic until the first string appears.
Key takeaways
+joins strings and creates a new string.- Variables go outside the quotes, and you supply the spaces yourself.
- Evaluation goes left to right, which matters when numbers and strings are mixed.
- The
sinterpolator is a tidier alternative for longer messages.
