DEV Community

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

Posted on • Edited on

2

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.

Follow me for more such content

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay