Data types and variables
Programs work with data, and Scala gives that data types. The ones you will use most are:
Int: whole numbers such as3and-12;Double: numbers with a fractional part, such as3.14;Char: a single character in single quotes, such as'a';String: text in double quotes, such as"John Doe";Boolean: eithertrueorfalse.
The type matters because it decides what you can do with a value. The number 12 and the string "12" look alike, but 12 + 1 is 13 and "12" + 1 is "121".
A variable is a name attached to a value stored in memory. You can picture a box with a label: the value is inside, and the name is the label you use to find it again.
Declaring a variable
A declaration starts with val or var, then the name, then optionally a colon and the type, then = and the initial value:
val name: String = "AlgoCademy"
val age = 10
The first line states the type, String, after the name. The second leaves it out, and Scala infers Int from the value. Both are correct. Writing the type is useful for documentation; leaving it out keeps simple code short.
Once a variable exists, use its name to read its value:
val name = "AlgoCademy"
val age = 10
println(name)
println(age)
AlgoCademy
10
Always give a variable a value when you declare it. In Scala a local variable cannot be declared without one.
val versus var
Scala has two keywords for variables:
valmakes a value that can't be reassigned after it is set.varmakes a variable that can be reassigned.
val pi = 3.14
var score = 0
score = 10
Assigning to score is fine. Assigning to pi, as in pi = 3.2, is a compile error: Reassignment to val pi. Prefer val by default, because a value that never changes is easier to reason about, and switch to var only when the value genuinely needs to change.
Gotchas
- The type is fixed. A variable declared as
Intcan't later hold a string. Aftervar x = 5, the assignmentx = "five"fails to compile with a message that it found aStringwhere anIntis required. - The type must match the value.
val age: Int = "30"is an error for the same reason. Numbers are written without quotes;"30"is a string. - Reassign without the keyword. After
var age = 30, change it withage = 31. Writingvar age = 31again would try to declare a second variable of the same name. - Increment with
+=. Scala has no++operator. Writecount += 1.
The exercise
The exercise has three steps, each on its own line:
- Declare a
valnamednameof typeStringwith the value"Michael". - Declare a
varnamedageof typeIntwith the value30. It is avarbecause the next step changes it. - Reassign
ageto31. The assignment uses only=, with no keyword, and is allowed becauseageis avar.
val name: String = "Michael"
var age: Int = 30
age = 31
Key takeaways
- A variable is a name for a stored value, and every value has a type.
- Declare with
valfor values that stay fixed andvarfor values that change. - The type can be written after a colon or inferred from the value.
- Reassigning a
val, or giving a variable a value of the wrong type, is a compile error.
