What a variable is
A variable is a named container for a value. The program keeps the value in memory, and the variable's name is the label you use to get it back. Without variables, a program would have to repeat every value each time it needed it, and it couldn't keep results for later.
Data types
Every value in Kotlin has a type. The ones you meet first are:
Intfor whole numbers such as3and-12;Doublefor numbers with a fractional part, such as3.14;Charfor a single character, written in single quotes, such as'a'or'?';Stringfor text, such as"John Doe";Booleanfor one of two values,trueorfalse.
Type names start with a capital letter in Kotlin. The type decides what you can do with a value: you can do arithmetic on the number 12, but not on the string "12".
Declaring a variable with val and var
Kotlin has two keywords for creating a variable. Both are followed by the name, an optional type after a colon, and an initial value after =:
val name: String = "AlgoCademy"
var age: Int = 10
valmakes a read-only variable. Once it has a value, it can't be reassigned.varmakes a variable whose value you can reassign later.
Use val by default, and switch to var only when the value really must change. A val tells the reader that the value stays the same, and it lets the compiler catch accidental changes:
val name = "AlgoCademy"
var age = 10
age = 11
name = "Other"
The assignment age = 11 is fine. The last line doesn't compile, and the compiler reports 'val' cannot be reassigned.
Type annotations and type inference
The type after the colon is optional when you give an initial value. Kotlin works out the type from the value, which is called type inference. These two lines are equivalent:
val name: String = "Michael"
val name = "Michael"
Both give name the type String. The type is then fixed. A variable declared as an Int can't later hold a string:
var age: Int = 30
age = "31"
The second line is a compile error: assignment type mismatch: actual type is 'String', but 'Int' was expected. Likewise, val age: Int = "30" fails with initializer type mismatch: expected 'Int', actual 'String'.
Whole-number literals are Int when they fit in 32 bits. A literal that is too big, such as 3000000000, is a Long, a 64-bit integer type.
Using a variable
Once a variable exists, you use its name wherever you need its value:
val name: String = "AlgoCademy"
val age: Int = 10
println(name)
println(age)
AlgoCademy
10
Printing a variable shows its value, not its name. Inside a string, $name inserts the value: println("Welcome to $name") prints Welcome to AlgoCademy.
Always initialize
Create a variable with its first value. In Kotlin this is more than good style, because the compiler checks it. A local variable that is read before it has a value does not compile:
val age: Int
println(age)
The compiler reports variable 'age' must be initialized.
Naming and other mistakes
- Quotes around a number.
"30"is aString. For a number, write30. - Repeating
varwhen reassigning.var age = 31in the same scope tries to declare a second variable with the same name, which the compiler rejects as conflicting declarations. To change the value, writeage = 31. - Case matters.
ageandAgeare different names, and a misspelled name gives anunresolved referenceerror. - Mixing up the colon and the equals sign. The type goes after a colon, and the value goes after
=.
The exercise
The lesson has three steps, each one line long.
- Declare a read-only variable named
name, with the typeStringand the value"Michael". Useval. - Declare a mutable variable named
age, with the typeIntand the value30. Usevar, and no quotes around the number. - Reassign
ageto31with a plain=. No keyword is needed, because the variable already exists. This is allowed becauseageis avar.
Together they make:
val name: String = "Michael"
var age: Int = 30
age = 31
Key takeaways
- A variable is a named container for a value, and every value has a type.
valis read-only,varcan be reassigned, andvalis the default choice.- The type goes after a colon, and Kotlin can infer it from the initial value.
- A variable's type is fixed, so assigning a value of another type is a compile error.
- Reassigning uses
=alone, withoutvalorvar.