Variables and type inference

Kotlin infers the type of variable from the value assigned to it. In the following example, intVariable is assigned a type of Int:

    //Type Inference
var intVariable = 10

Since Kotlin is type-safe, the following snippet will result in a compilation error if uncommented:

    //intVariable = "String" 
//If uncommented -> Type mismatch:
//inferred type is String but Int was expected
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset