Types
Int, Double, String, Boolean — meet each one with its own example.
1. Int — whole numbers
Int represents whole numbers (no decimals). Arithmetic uses the usual operators, plus % for the remainder:
val a: Int = 10
val b = 3 // type inferred as Int
a + b // 13
a - b // 7
a * b // 30
a / b // 3 ← integer division: drops the fractional part
a % b // 1 ← remainder of a / b
Note a / b truncates toward zero — 10 / 3 is 3, not 3.33. Use Double if you need decimals.
2. Int — area
Implement area(width, height): Int returning width * height.
Starter code
fun area(width: Int, height: Int): Int {
return 0
}3. Double — decimal numbers
Double represents decimal numbers (64-bit floating point). Same operators as Int but / is real division, not truncating:
val pi: Double = 3.14159
val r = 2.5 // type inferred as Double (because of the decimal)
pi * r * r // 19.6349375
10.0 / 3.0 // 3.3333333333333335
Mixing Int and Double produces a Double:
1 + 2.0 // 3.0 (Double)
Decimal literals (2.0, 0.5) are Double by default.
4. Double — average
Implement average(a, b): Double returning (a + b) / 2.0.
Starter code
fun average(a: Double, b: Double): Double {
return 0.0
}5. String — text
String holds text. Some everyday operations:
val name = "Ada"
name.length // 3
name.uppercase() // "ADA"
name.lowercase() // "ada"
name + "!" // "Ada!" (concatenation)
"Hi, $name!" // template (idiomatic)
Indexing returns a Char:
name[0] // 'A' (Char, not String)
name.first() // 'A'
Strings are immutable — these methods return new strings, they never modify the original.
6. String — shout
Implement shout(s) returning s in uppercase followed by an exclamation mark. Hint: s.uppercase() + "!".
Starter code
fun shout(s: String): String {
return s
}7. Boolean — true or false
Boolean is the simplest type — only two values, true and false.
You rarely write a Boolean literal directly; instead, comparisons produce one:
val older = age >= 18 // Boolean
val same = name == "Ada" // Boolean
val empty = name.isEmpty() // Boolean
The full set of comparison operators: ==, !=, <, >, <=, >=. They work on numbers and (for equality) on strings and other types.
The next lesson covers how to combine booleans with &&, ||, and !.
8. Boolean — isPositive
Implement isPositive(n) returning true if n is greater than zero. Use a comparison.
Starter code
fun isPositive(n: Int): Boolean {
return false
}Run this Kotlin lesson in your browser
SnipDojo ships an in-browser Kotlin editor with the exercises above already wired to tests. Sign in with Google to edit the starter code, run it, and track your progress toward the free certificate — no installs and no local toolchain.
Sign in to run this code →