Type checking and automatic casting

In the Java programming language, if a function accepts a parameter of type Objectand if the actual type of that parameter needs to be checked, then the instanceof keyword is used. But this alone wasn't sufficient; even if the type of the parameter is found it still needs to be explicitly cast to access any properties or functions within it. This is solved in Kotlin as follows:

if (csv is String) {
return csv.length;
}

In the preceding code, the if keyword is used to check whether the csv parameter is of type String or not. If the condition returns true, Kotlin does the implicit casting of csv to String to enable access to properties or functions and to reduce the chances of errors while keeping the code concise. 

..................Content has been hidden....................

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