
Android Development with Kotlin
By :

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 the expression, we have a string, Kotlin
and we are assigning it to a variable, title
, defined on the left-hand side of the expression.
We specified a variable type as String
, but it was obvious, because this is the same type as the type of assigned expression...