Step 1 of 5 · Reading · ~3 min
Learn
Getting Started
val and var
Kotlin gives you two ways to introduce a name, and it very much wants you to pick the boring one.
| Keyword | Can you reassign it? | Use it when |
|---|---|---|
val | no — assigned once | almost always; this is the default |
var | yes | the value genuinely has to change (a counter, a running total) |
fun main() {
val name = "Ada"
var visits = 0
visits = visits + 1
visits += 1
println("$name has visited $visits times")
}
//> Ada has visited 2 times
Try to reassign a val and the program does not build:
val name = "Ada"
name = "Grace" // error: val cannot be reassigned
That is a compile error, not a runtime one. The bug never reaches anybody.
What does val actually freeze?
Hold the right mental model from the start, because this is where beginners go wrong later:
val freezes the name, not the thing the name points at.
A val is a label glued to one object. The label cannot be moved to a different object. If the
object itself has moving parts, those parts still move. A String and an Int have no moving
parts, so the distinction never bites here — it matters enormously for collections, which is
where the Lists and Arrays lesson picks it up.
Types: inferred, but still there
Kotlin is statically typed. Every name has a type fixed at compile time; you just rarely have to write it down.
val city = "Oslo" // String
val year = 2016 // Int
val ratio = 0.75 // Double
val ready = true // Boolean
val initial = 'K' // Char — single quotes, exactly one character
There is one case where you must write the type: declaring a name before you have a value for it, because there is nothing to infer from.
val label: String
label = "ready"
The core types are Int, Long, Double, Float, Boolean, Char and String. Kotlin has
no int versus Integer split like Java's — Int is one type, and the compiler uses a machine
primitive underneath wherever it can.
✓ / ✗
✓ Reach for val first and let the compiler tell you when you actually need var:
val total = 3 + 4
✗ Declaring everything var "just in case" throws away help you were being given for free:
var total = 3 + 4 // nothing ever reassigns it
Reading input, and that !!
Every exercise here reads from standard input with readLine(). It hands back a String? — a
string or nothing at all, because the input may have run out. The !! in the starters means
"I promise a line is there; blow up if I am wrong":
val a = readLine()!!.toInt()
readLine()!! gives you a plain String, and .toInt() turns the text "7" into the number
7. Skip the conversion and you still hold text — and + on text glues rather than adds. The
Null Safety lesson takes !! apart properly and shows the safer alternatives; for now read it
as the starter's way of saying "the input is guaranteed".
Your exercise
Sum Two Numbers hands you a main that already reads two lines and converts each to Int.
Add one statement that prints their sum.
The first visible test feeds 7 then 5 and expects 12. The mistake the grader catches is
dropping the .toInt() conversions: readLine()!! + readLine()!! concatenates text, so that
test prints 75 and fails. A hidden test feeds -3 and 3 and expects 0, so do not assume
the values are positive — and print the number on its own, with no Sum: in front of it.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…