February 2018
Intermediate to advanced
356 pages
9h 10m
English
Kotlin supports the feature called Smart Casts which enables developers to use the cast operators automatically. After checking the variable type, in Java, the cast operator must be explicit. Let's check it out:
fun returnValue(instance: Any): String { if (instance is String) { return instance } throw IllegalArgumentException("Instance is not String")}
As we can see, the cast operator is not present anymore. After checking the type, Kotlin can infer the expected type. Let's check the Java version for the same piece of code:
public String returnValue(Object instance) { if (instance instanceof String) { String value = (String) instance; return value; } throw IllegalArgumentException("Instance is not String");}
It makes the cast ...