October 2018
Intermediate to advanced
370 pages
9h 15m
English
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
Read now
Unlock full access