Kotlin is a modern, concise, and type-safe programming language used for Android development, backend systems, desktop apps, and more. One of the core foundations of Kotlin is how it handles variables and data types. Understanding these is essential because they define how your program stores and manipulates information.
This post covers:
A variable is a container that stores data in memory.
In Kotlin, you define a variable using two keywords:
val → immutable (cannot be changed)var → mutable (can be changed)Kotlin encourages immutability because it makes your code safer and more predictable.
val)Use val when the value should not change after being assigned.
val name = "Luke"
val age = 30 Here:
name and age cannot be reassigned.Immutability is best practice for most variables.
var)Use var when the value needs to change.
var score = 10
score = 20 // ✔ allowed Use var for variables like counters, states, and temporary values.
Kotlin is a statically typed language, meaning variable types are known at compile time.
However, you do not always need to specify the type because Kotlin can infer it.
val city = "Lagos" // inferred as String
val temperature = 28 // inferred as Int If you want, you can explicitly set the type:
val city: String = "Lagos"
val temperature: Int = 28 Kotlin includes several built-in primitive data types, but unlike Java, they are all objects under the hood (no primitive vs wrapper distinction).
| Type | Range |
|---|---|
Byte | -128 to 127 |
Short | -32,768 to 32,767 |
Int | -2^31 to 2^31-1 |
Long | -2^63 to 2^63-1 |
val count: Int = 100
val population: Long = 7_800_000_000 The underscore _ improves readability.
| Type | Precision |
|---|---|
Float | 6-7 decimal digits |
Double | 15-16 decimal digits |
val percentage: Float = 99.5F
val pi: Double = 3.14159265358979 Note the F suffix for Float.
Char)Represents a single Unicode character.
val initial: Char = 'K' Characters use single quotes.
Boolean)Stores logical values: true or false.
val isOnline: Boolean = true
val isSubscribed = false Strings are sequences of characters enclosed in double quotes.
val name = "Lawson" Kotlin also supports string templates, allowing you to insert variables directly:
val age = 25
println("My name is $name and I am $age years old.") For expressions, use ${ }:
println("Next year I will be ${age + 1}") Use triple quotes """:
val message = """
Hello Kotlin!
Welcome to multiline strings.
""".trimIndent() An array stores multiple values of the same type.
val numbers = arrayOf(1, 2, 3, 4)
val names = arrayOf("Luke", "James", "Ada") Access elements:
println(numbers[0]) // prints 1 Modify elements:
numbers[1] = 20 Kotlin collections come in two major types:
listOf()setOf()mapOf()mutableListOf()mutableSetOf()mutableMapOf()val fruits = listOf("Apple", "Orange") // cannot change
val numbers = mutableListOf(1, 2, 3) // can change
numbers.add(4) Kotlin has built-in protection against null pointer exceptions.
By default, variables cannot hold null.
val name: String = "Luke"
// name = null ❌ error To allow null, you must add ?
var middleName: String? = null You must safely handle nullable variables using:
?. (safe call)?: (Elvis operator)!! (non-null assertion – risky)println(middleName?.length) // safe call (returns null)
println(middleName ?: "N/A") // Elvis (default value) Kotlin does not automatically convert between different types.
To convert:
val num = "123".toInt()
val amount = 45.toDouble() Common conversion functions:
toInt()toLong()toFloat()toDouble()toString()fun main() {
val name: String = "Kotlin Student"
var score: Int = 90
val pi = 3.14 // inferred as Double
val isActive = true
var nickname: String? = null
val numbers = mutableListOf(1, 2, 3)
numbers.add(4)
println("Name: $name")
println("Score: $score")
println("PI: $pi")
println("Active: $isActive")
println("Nickname: ${nickname ?: "No nickname"}")
println("Numbers: $numbers")
}
| Concept | Explanation |
|---|---|
val | Immutable variable |
var | Mutable variable |
| Type inference | Kotlin automatically detects types |
| Basic Types | Int, Double, Boolean, Char, String |
| Nullable types | Use ? to allow null |
| Collections | Immutable vs mutable lists, sets, maps |
| Type conversion | Manual conversions needed |
Latest tech news and coding tips.
In JavaScript, it’s commonly used for: Recursive functions (like Fibonacci) Heavy calculations Repeated API/data processing…
For years, responsive design has depended almost entirely on media queries. We ask questions like: “If…
1. What is Task Scheduling? Task scheduling is the process of automatically running commands, scripts,…
Here’s a comprehensive, clear differentiation between a Website and a Web App, from purpose all the…
Visual Studio Code (VS Code) is powerful out of the box, but its real strength…
1. What Is a Variable in JavaScript? A variable is a named container used to store data…