DEV Community

Cover image for Type Inference in Kotlin. How things exactly work behind the scenes.
Jay Tillu😎
Jay Tillu😎

Posted on • Updated on

Type Inference in Kotlin. How things exactly work behind the scenes.

Type — Data Type
Inference — To automatically identify something.

  • kotlin is a strongly typed language that supports Type Inference.

  • Kotlin compiler can automatically identify the data type of the variable. The compiler knows this by the initializer.

  • So if you initialize the value on the declaration, you don’t need to define data type explicitly.

  • Because every primitive type has a default value. The compiler will directly assume the size and type to a variable by that.

  • For example, if you assign any whole number to a variable, the compiler will directly assume that the variable is of type Int.

  • But remember if you want to initialize the value to the variable later, then, in that case, you have to define the data type explicitly. In that case, the concept of type inference won’t work.

So, guys, that’s it for type inference. This is how all the magic works behind the scenes. Feel free to ask any questions.

Till then Keep Coding, Keep Loving.

Jai Hind, Vande Mataram 🇮🇳

Wanna get in touch with me? Here are links. I’ll love to become your friend. 😊

Top comments (0)