Quotes make the difference
In Scala, the same digits can be a number or text depending on the quotes around them. 15 is an Int, a whole number. "15", with double quotes, is a String: two characters that happen to be digits. They look alike but behave differently.
val count = 15 + 1 // 16, arithmetic on numbers
val text = "15" + 1 // "151", text joined with the digit 1
Adding two numbers gives their sum, 16. Adding a string and a number builds a longer string, "151", because + on a string means concatenation.
Types you write and types Scala infers
You don't always have to write the type. In val n = 15 Scala sees an integer literal and decides n is an Int. In val s = "15" it sees a string literal and decides s is a String. Either way the type is fixed from then on, and a later line can't store text in n or a number in s.
Declared types are enforced
Scala checks types when it compiles the program. If you write the type after the name, the value on the right has to be of that type. Here is the lesson's starting code:
val age: Int = "29"
val phone: String = 2025550153
Both lines are rejected, and the compiler explains each one:
-- [E007] Type Mismatch Error ---------------------------------------------
| val age: Int = "29"
| ^^^^
| Found: ("29" : String)
| Required: Int
-- [E007] Type Mismatch Error ---------------------------------------------
| val phone: String = 2025550153
| ^^^^^^^^^^
| Found: (2025550153 : Int)
| Required: String
Found is the type the value has and Required is the type the declaration asked for. Scala does not quietly turn the text "29" into a number, nor the number into text.
Fixing the exercise
- Keep the type annotations.
IntandStringstate what each value is supposed to be, and they are correct. - Fix
age. An age is a quantity you may calculate with, such as next year's age, so it is a number. Remove the quotes:29. - Fix
phone. A phone number is an identifier. You never add two phone numbers, and a leading zero or a plus sign would be part of it. Put it in quotes so it is text.
The solution in Scala
val age: Int = 29
val phone: String = "2025550153"
A good rule for choosing: if you might do arithmetic with the value, use a number. If it only names or identifies something, use a string.
Gotchas
- Digits in quotes are still text.
"29" + 1is"291", not 30. - Not every number fits in an
Int. AnIntholds values up to 2147483647. Longer numbers need aLong, written with anL, as in12025550153L. Storing a phone number as text avoids the question. - Multiplying strings.
"ab" * 3repeats the text and gives"ababab", but"ab" * "cd"is a type error, because the right side of*has to be anInt. - Converting is explicit. To get a number out of a string, call a method such as
"29".toInt. A later lesson covers conversion.
Key takeaways
- No quotes means a number, double quotes mean a
String. - A declared type must match the value, and the compiler reports
FoundandRequiredtypes when it doesn't. - Use numbers for quantities you calculate with and strings for identifiers such as phone numbers.
