Extension functions

It is also possible to call Kotlin's extension function in Java. In Kotlin, create an extension function that takes one parameter, multiplies the value by 2, and returns the result. See the following Kotlin extension function:

fun Int.doubleTheValue() = this * 2

Create this extension function in the Kotlin class and call this function into Java's main function by using the CallKotlinKt.doubleTheValue function. See the following example:

public static void main(String args[]) {
int i = 5;
int result = CallKotlinKt.doubleTheValue(i);
System.out.print("Kotlin's Extension function, Multiply "+ i +" with 2 = "+ result);
}

 As a result, the output will be as expected:

Kotlin's Extension function, Multiply 5 with 2 = 10
..................Content has been hidden....................

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