Variables and Data Types
Variables and Data Types
Video Explanation

Declaring Variables
Kotlin has two keywords for declaring variables:
| Keyword | Meaning | Mutable? |
|---|---|---|
val | Value (constant) | No |
var | Variable | Yes |
fun main() {
val name = "Alice" // immutable — cannot be changed
var age = 25 // mutable — can be changed
age = 26 // OK
// name = "Bob" // ERROR: val cannot be reassigned
println("$name is $age years old")
}
Type Inference
Kotlin can infer types automatically:
val city = "Mumbai" // Kotlin infers String
val population = 20_000_000 // Kotlin infers Int
val temperature = 36.5 // Kotlin infers Double
val isCapital = false // Kotlin infers Boolean
Explicit Type Declaration
You can also declare types explicitly:
val name: String = "Kotlin"
val version: Double = 1.9
val year: Int = 2024
val isActive: Boolean = true
Basic Data Types
Integer Types
val byte: Byte = 127 // -128 to 127
val short: Short = 32767 // -32,768 to 32,767
val int: Int = 2_147_483_647 // ~2.1 billion
val long: Long = 9_223_372_036_854_775_807L
Floating-Point Types
val float: Float = 3.14f // 32-bit, note the 'f' suffix
val double: Double = 3.14159 // 64-bit (default for decimals)