Kotlin
Beginner
1 min read
val, var and Type Inference
Example
fun main() {
// Immutable reference — cannot be reassigned
val pi = 3.14159
val greeting: String = "Hello"
// Mutable reference
var counter = 0
counter += 1
counter++
// Explicit types
val bigNumber: Long = 9_000_000_000L
val hex: Int = 0xFF
val binary: Int = 0b1010_1010
// Type conversion — no implicit widening
val intVal: Int = 42
val longVal: Long = intVal.toLong()
val doubleVal: Double = intVal.toDouble()
println("pi=$pi counter=$counter")
println("long=$longVal double=$doubleVal")
// Late-initialised var (non-null, initialised before first use)
lateinit var name: String
name = "Kotlin"
println("Language: $name")
// Compile-time constants
// const val MAX = 100 // only allowed at top level or in object
}
const val APP_VERSION = "1.0.0" // top-level const
Related Resources
Kotlin Reference
Complete tag & property list
Kotlin How-To Guides
Step-by-step practical guides
Kotlin Exercises
Practice what you've learned
More in Kotlin