AI Features

Kotlin's Type Inference

Understand Kotlin's powerful type inference capabilities and how to use them to write more succinct code.

Type inference is a compiler feature that allows you to omit types in your code when the compiler can infer it for you.

Type Inference in Kotlin

Kotlin’s compiler can infer the types of most variables, so adding the type is optional:

Kotlin
// Run the code to see the variable's types
val string = "Educative"
val int = 27
val long = 42L
val double = 2.71828
val float = 1.23f
val bool = true

Note: The terms on the left-hand side of the equals sign are just the variable names, not the data types. ...

Type Inference for