October 2018
Intermediate to advanced
370 pages
9h 15m
English
Any is a parent or a superclass of all classes in Kotlin. If our class is not derived from any class, then it has Any as a super class. All data types including Integer, Float, Double, and so on are derived from an Any class. (We will learn more about this in Chapter 3, The Four Pillars of Object-Oriented Programming). The following declarations are valid in Kotlin:
var any : Any? = nullany = 1234 // integerany = "Hello" // Stringany = 123.456 // Double
To understand the importance of smart casting, let's create a function with one parameter of the nullable Any? type:
fun mySmartCast(any :Any?){ if(any is Int) { var i = any + 5 println("Value is Int $i") } else if(any is String) { var s = "Hello " + any println("Value is String ...
Read now
Unlock full access