Just as Java can be used seamlessly in Kotlin, Kotlin can just as easily be used from your Java programs. The following sections will cover some ways in which Kotlin code can be reused in other projects written in Java.
Kotlin from Java
Top-level functions
The JVM does not support top-level functions. Therefore, to make them work with Java, the Kotlin compiler creates a Java class with the name of the 'kt' file with the first letter being capitalized. The functions are then defined as Java methods on this class, which must be instantiated before use.
For example, consider the following top-level function:
package com.packt.chapter4 fun cube(n: Int): Int = n * n * n
For this, the Kotlin compiler will generate...