Type inference

As we saw in previous examples, unlike Java, the Kotlin type is defined after the variable name:

    var title: String 

At first glance, this may look strange to Java developers, but this construct is a building block of a very important feature of Kotlin called type inference. Type inference means that the compiler can infer type from context (the value of an expression assigned to a variable). When variable declaration and initialization is performed together (single line), we can omit the type declaration. Let's look at the following variable definition:

    var title: String = "Kotlin" 

The type of the title variable is String, but do we really need an implicit type declaration to determine variable type? On the right side of ...

Get Android Development with Kotlin now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.