What you'll learn
Every program stores and manipulates data. This module covers how Kotlin holds values in variables, its type system, and the string templates that make Kotlin code so readable.
By the end you'll be able to:
- Declare variables with
valandvar— and know which to prefer - Use Kotlin's basic types and let the compiler infer them
- Build strings cleanly with string templates
- Convert between types explicitly
- Define compile-time constants with
const val
val vs var
Kotlin has two ways to declare a variable, and choosing between them is a real design decision. val creates a read-only reference (assigned once); var creates a mutable one. Notice there's no type keyword and no new — you just name it and assign:
val — read-only
Assigned once, never reassigned. Kotlin's default — prefer it for safer, more predictable code.
var — mutable
Can be reassigned. Use only when the value genuinely needs to change.
val name = "Sara" // read-only
var age = 25 // mutable
age = 26 // OK — var can change
// name = "Omar" // ERROR — val cannot be reassigned
println(name)
println(age)Prefer val
val by default, and only switch to var when you truly need to reassign. Immutable values are easier to reason about and prevent a whole class of bugs.Types & inference
Kotlin is statically typed — every value has a type known at compile time — but you rarely need to write the type out, because the compiler infers it from the value. You can state it explicitly with val name: Type when it aids clarity:
val count: Int = 42 // explicit type
val price = 19.99 // inferred as Double
val letter = 'K' // Char
val isReady = true // Boolean
val big = 1_000_000L // Long (note the L)
println(count + big)| Type | Holds | Example |
|---|---|---|
Int | whole numbers (the default) | val n = 42 |
Long | large whole numbers | val n = 42L |
Double | decimals (the default) | val p = 3.14 |
Float | smaller decimals | val p = 3.14f |
Char | a single character | val c = 'K' |
Boolean | true or false | val ok = true |
String | text | val s = "hi" |
Note
String templates
Building text from values is a joy in Kotlin. A string template uses $ to insert a variable, and ${...} to insert any expression:
val name = "Sara"
val age = 25
println("Name: $name") // simple variable
println("Next year: ${age + 1}") // an expression needs braces
println("Uppercase: ${name.uppercase()}") // a method callTip
$name form for a bare variable, and the ${...} form whenever you need an expression, a method call, or a property access. It's cleaner than concatenating with +.Type conversion
Unlike Java, Kotlin does not automatically convert between number types — you must be explicit with methods like toInt(), toDouble(), or toString(). This prevents subtle bugs from surprise conversions:
val text = "42"
val number = text.toInt() // String -> Int
println(number + 8) // 50
val x = 10
val y = 3.0
println(x.toDouble() / y) // explicit conversion requiredConstants
For a value that's a genuine compile-time constant (known before the program runs, at the top level or in a companion object), use const val. By convention, constants are named in UPPER_SNAKE_CASE:
const val MAX_USERS = 100
const val APP_NAME = "MasterCoding"Note
val can be set at runtime; a const val must be known at compile time (so it can only hold primitives and strings). Reach for const for fixed configuration values.Recap & quick check
Key takeaways
- val is read-only (preferred); var is mutable — reach for val by default.
- Kotlin is statically typed but infers types, so you rarely write them out.
- Everything is an object; Int, Double, Boolean, Char, String are the everyday types.
- String templates: $name for a variable, ${...} for any expression.
- Kotlin never auto-converts number types — use toInt(), toDouble(), etc. explicitly.
Quick check
1. Which keyword declares a read-only variable?
2. Do you have to write the type when declaring a variable?
3. How do you insert an expression into a string?
4. How do you convert a Double to an Int in Kotlin?
5. Which should you prefer by default?
Great — you can store and shape data now. Next up: Module 3 — Null Safety, Kotlin's most celebrated feature.