What is type inference in Kotlin, and how does the compiler decide the type of `val n = 1`?
answer
- Type comes from the right-hand initializer
- Integer literal default = Int
- Static + strong typing, only the annotation is omitted
- val n = 1 == val n: Int = 1
- Most-specific suitable type
basics
~20 sType inference lets you skip writing the type. The compiler reads the value on the right and figures out the type itself. For val n = 1 it sees a whole number, so n is an Int.
solid answer
~40 sType inference means the compiler derives a variable's static type from its initializer expression instead of you annotating it. For `val n = 1`, the literal `1` is an integer literal whose default type is `Int`, so `n` is inferred as `Int`. Inference is purely compile-time: Kotlin stays statically and strongly typed, so `n` is exactly as type-safe as `val n: Int = 1`. It works for local variables and for properties with an initializer. The inferred type is the most specific suitable type the compiler can determine from the right-hand side: `"hi"` gives `String`, `1.0` gives `Double`, `listOf(1, 2)` gives `List<Int>`. You can always override inference with an explicit annotation, e.g. `val n: Long = 1` or `val n: Number = 1`, when you want a wider or different type.
code
kotlin · 5 linesval n = 1 // Int
val price = 9.99 // Double
val name = "Ada" // String
val ids = listOf(1, 2, 3) // List<Int>
// name = 5 // error: name is Stringgo deeper
Knows that the type comes from the value and that val n = 1 is an Int.
Explains it's compile-time, that Kotlin stays statically/strongly typed, and lists default literal types.
Frames inference as picking the most-specific suitable type and notes equivalence to explicit annotation in bytecode/safety.
Discusses inference as a readability/maintainability lever and the trade-offs of relying on it in public APIs vs locals.
## What type inference is **Type inference** is the compiler's ability to determine the static type of a declaration from the **initializer** (the expression on the right of `=`) so you don't have to write the type yourself. Kotlin is **statically typed** (every expression has a type known at compile time) and **strongly typed** (types are enforced); inference only removes the *typing*, never the *checking*. ```kotlin val n = 1 // inferred Int — identical to: val n: Int = 1 val s = "hi" // inferred String val d = 1.0 // inferred Double val flag = true // inferred Boolean ``` ## How `val n = 1` becomes Int - `1` is an **integer literal**. An undecorated integer literal that fits in 32 bits has the **default type `Int`**. - The compiler picks the most specific type that fits the expression, so `n` is `Int`, not `Number` or `Long`. - After inference, `n` behaves exactly like an explicitly typed `Int`; you get the same autocomplete, the same errors, the same generated bytecode. ## Default literal types worth remembering - Integer literal → `Int` (or `Long` if it doesn't fit in `Int`, or with the `L` suffix: `1L`). - Floating literal → `Double` (or `Float` with the `f`/`F` suffix: `1.0f`). - `'a'` → `Char`; `"a"` → `String`; `true`/`false` → `Boolean`. ## Where inference applies — and where it doesn't - **Applies:** local `val`/`var` with an initializer, and properties with an initializer. - **Does NOT apply / annotation required:** an uninitialized `lateinit`/abstract property, a property/parameter with no initializer, and (for clarity/recursion) certain function return types. More on that in follow-ups. ## Inference keeps you type-safe ```kotlin val n = 1 // n = "oops" // compile error: n is Int, cannot assign String ``` The takeaway: omitting the type is a convenience, not a loss of safety — the type is still fixed at compile time.
- Does inference make Kotlin dynamically typed like Python?No. The type is fixed at compile time; inference only omits the annotation. Kotlin stays statically and strongly typed.
- What type is `val x = 1.0`?`Double` — an undecorated floating-point literal defaults to `Double`, not `Float`.
Like a tailor sizing a suit from the person standing in front of them instead of asking for measurements — the fit is exact, you just didn't have to state the numbers.
saying these in an interview costs you the question
- Claiming inference makes Kotlin dynamically typed
- Saying the type can change at runtime
- Thinking `val n = 1` is `Long` or `Number` by default
- Believing inference is slower at runtime (it's compile-time only)