Kotlin, lesson 2
A variable is a name for a value. Kotlin gives you two keywords for making one, and the difference matters:
val makes a read-only variable. You assign it once and it never changes.var makes a mutable variable. You can assign it a new value later.Start with val and only switch to var when you really need to change the value. Code
where most names never change is easier to follow, and the compiler stops you from changing
something by accident.
Every value in Kotlin has a type, and the compiler always knows it. Usually you don't have to write the type, because Kotlin infers it from the value you assign:
| Value | Inferred type | What it holds |
|---|
2 exercises to try what you just read.
42 | Int | whole numbers, about ±2.1 billion |
3_000_000_000 or 7L | Long | much bigger whole numbers |
9.99 | Double | numbers with a decimal point |
true | Boolean | true or false |
'K' | Char | a single character |
"Kotlin" | String | text |
You can write the type yourself after a colon. This is useful when the inferred type is not the one you want:
Underscores in number literals are ignored, so 1_000_000 is just a readable one million.
Kotlin never quietly turns one number type into another. An Int cannot be stored in a
Double variable without you asking:
Every number type has conversion functions such as toInt(), toLong() and toDouble().
This strictness feels fussy at first, but it means you always know exactly what kind of
number you are working with.
Choosing the right type matters because an Int has a limit. If a calculation goes past
about 2.1 billion, the value wraps around to a negative number without any error. Use Long
when results can get that big.
A value that is fixed for the whole program, such as the number of days in a week, can be a
compile-time constant with const val. Constants must be declared outside any function
(at the top level of the file), and they can only hold a String or a basic type like Int
or Double.
Kotlin style uses camelCase for variables and functions: totalPrice, visitorCount.
Constants use SCREAMING_SNAKE_CASE: MAX_PLAYERS. Names can contain letters, digits and
underscores but can't start with a digit. Pick names that say what the value means;
secondsPerDay beats x.
The starter already reads two lines of input: a name and an age. Print a badge with three lines. The last line shows the age the person will be next year. Use string templates.
Input format: a name on the first line, a whole number age on the second.
Output format:
Sample input:
Sample output:
There are 31,536,000 seconds in a (non-leap) year. Declare that number as a top-level
const val SECONDS_PER_YEAR, then print how many seconds are in the number of years given
as input.
Careful: for 100 years the answer is bigger than an Int can hold. Make sure your
constant (and so the result) is a Long.
Input: one whole number of years. Output: one number.
Sample input:
Sample output:
fun main() {
val city = "Lisbon"
var visitors = 120
visitors = visitors + 30 // fine: visitors is a var
// city = "Porto" // compile error: val cannot be reassigned
println("$city had $visitors visitors")
}val score: Int = 0
val distance: Long = 5 // a Long, even though 5 would fit in an Int
val price: Double = 4.0val apples = 3
// val weight: Double = apples // compile error: type mismatch
val weight: Double = apples.toDouble()const val DAYS_PER_WEEK = 7
fun main() {
val weeks = 4
println("$weeks weeks is ${weeks * DAYS_PER_WEEK} days")
}*** HELLO ***
Name: <name>
Next year: <age + 1>Ada
36*** HELLO ***
Name: Ada
Next year: 37263072000