Building messages from variables
Concatenation joins strings with +. It is most useful with variables, because the same code can then produce different messages depending on what the variables hold:
var adjective = "awesome"
var message = "AlgoCademy is "
message = message + adjective
println(message) // AlgoCademy is awesome
The right side, message + adjective, is evaluated first and produces a new string. The assignment then stores it in message, replacing the old value.
The += operator
Updating a variable with its own value plus something else is so common that Scala has a shorthand. message += adjective means exactly message = message + adjective:
var adjective = "awesome"
var message = "AlgoCademy is "
message += adjective
println(message) // AlgoCademy is awesome
You can use it as many times as you like to build a message in pieces:
var name = "Andy"
var pet = "dog"
var message = "Hey, "
message += name
message += "! Nice "
message += pet
message += "!"
println(message) // Hey, Andy! Nice dog!
Each += adds to the end of whatever message holds at that moment.
Why var matters here
+= reassigns the variable on its left, so that variable must be declared with var. A val can't be reassigned, and using += on one is a compile error: the compiler reports that += is not a member of String.
Strings are immutable in Scala. A += doesn't change the existing string; it creates a new, longer one and makes the var point to it. You can see this with a second variable:
val s = "ab"
val t = s + "c"
println(s) // ab
println(t) // abc
The right side of += doesn't have to be a string. Appending a number turns it into text:
var n = "n"
n += 5
println(n) // n5
Watch the order of evaluation when you mix numbers and text. + works left to right, so 1 + 2 + "a" is "3a", while "a" + 1 + 2 is "a12".
When to prefer something else
Long chains of += are easy to read in a lesson but clumsy in real code. For a message with a few variables in it, Scala's string interpolation, written s"Hey, $name! Nice $pet!", builds the same text in one expression. Repeated += in a loop also creates a new string each time, so for many pieces a StringBuilder is the usual tool. For the small messages in this lesson, += is exactly right.
The exercise
The starter code gives you two variables, a var message that holds "Coding is " and a val adjective that holds "worthwhile". The three steps are:
- Append
adjectivetomessagewith+=. Onlymessageis avar, andmessageis the one being changed, so this is allowed. Thevalis only read. - Append
", isn't it?"with+=. The apostrophe needs no escape inside double quotes, and the leading comma and space are part of the string. - Print
messagewithprintln.
The complete solution:
var message = "Coding is "
val adjective = "worthwhile"
message += adjective
message += ", isn't it?"
println(message)
It prints:
Coding is worthwhile, isn't it?
Common mistakes
- Forgetting the spaces. Concatenation adds nothing between the pieces. The space after
"Coding is "is what separatesisfromworthwhile. - Printing before appending.
printlnshows the value at that moment, so a print before the+=lines would showCoding isonly. - Using
+=on aval. The compiler rejects it. Declare the variable that grows withvar. - Putting the name in quotes.
message += "adjective"appends the word "adjective", not the contents of the variable.
Key takeaways
message += xis shorthand formessage = message + x.- The variable on the left must be a
var. - Strings are immutable: each
+=creates a new string. - Pieces are joined exactly as they are, so include any spaces and punctuation you need.
