Kotlin - Introduction Type inference

Introduction

Kotlin is a strongly typed language, but you don't always need to declare types explicitly.

The compiler tries to figure out the type of an expression from the information included in the expression.

This mechanism is called type inference.

Kotlin can figure out the type of the parameter(s) from the function signature.

It can be used in single-line functions where the return value can be inferred from the expression in the function:

fun add(x: Int) = x + 1 

You can explicitly annotate the type:

val explicitType: Number = 12.3