Kotlin Type Inference

Kotlin can figure out the data type of a variable on its own by looking at the value you assign. You do not always need to write the type manually. This feature is called type inference.

How Type Inference Works


You write:          val score = 95

Kotlin sees:        95 is a whole number that fits in an Int
                         │
                         ▼
Kotlin infers:      val score: Int = 95

You never wrote "Int", but Kotlin knows it automatically.

Examples of Type Inference

val name    = "Riya"         // inferred as String
val age     = 28             // inferred as Int
val height  = 5.7            // inferred as Double
val isAlive = true           // inferred as Boolean
val initial = 'R'            // inferred as Char

Each line works exactly the same as writing the type explicitly. Kotlin detects the type from the right side of the = sign.

When to Write the Type Explicitly

Type inference is not always enough. Declare the type explicitly in these situations:

When the value is assigned later

val status: String        // no value yet, type required
// ... some logic ...
// status = "active"      // assigned later

When you want a different type than Kotlin would infer

val small: Byte = 100     // Kotlin would infer Int, but you want Byte
val price: Float = 9.99f  // explicit Float instead of Double

When writing APIs or public code for readability

fun getScore(): Int {     // explicit return type for clarity
    return 95
}

Type Inference is Not Guessing

Kotlin's type inference is not random — it is precise and determined at compile time. Once Kotlin infers a type, it locks it in. You cannot assign a different type to the same variable later.

var count = 10       // inferred as Int
count = 20           // OK, still Int
count = "twenty"     // ERROR: String cannot be assigned to Int

Inference with Function Return Values

Type inference also works with the result of function calls and expressions:

val text = "hello".uppercase()    // inferred as String → "HELLO"
val length = text.length          // inferred as Int → 5
val doubled = length * 2          // inferred as Int → 10

Inference vs Explicit: Readability Guide


┌──────────────────────────────────────────────────────┐
│  Prefer inference when the type is obvious:          │
│  val message = "Done"       ← clearly a String       │
│  val count = 0              ← clearly an Int         │
│                                                      │
│  Prefer explicit when the type needs clarification:  │
│  val ratio: Double = 1      ← looks like Int         │
│  val flag: Boolean = false  ← makes intent clear     │
└──────────────────────────────────────────────────────┘

Practical Example

fun main() {
    val city = "Mumbai"        // String
    val pinCode = 400001       // Int
    val latitude = 19.0760     // Double
    val isCoastal = true       // Boolean

    println("City: $city")
    println("PIN: $pinCode")
    println("Latitude: $latitude")
    println("Coastal city: $isCoastal")
}

Output:

City: Mumbai
PIN: 400001
Latitude: 19.076
Coastal city: true

Kotlin inferred all four types correctly without any explicit annotations. The code remains clean and easy to read.

Leave a Comment

Your email address will not be published. Required fields are marked *