October 2018
Intermediate to advanced
370 pages
9h 15m
English
We can call the Kotlin function in Java by using the filename as a reference. We also need to add the kt keyword with the filename; for example, KotlinToJavakt. However, Kotlin makes it possible to assign different names to our file and function names. Create a new class named CallKotlinUtil.kt and add the following code:
@file:JvmName("KotlinUtil")package Chapter08.CallKotlinFromJavafun addition (a: Int, b : Int){ println("Result of $a + $b is ${a+b}")}
Use the @file:JvmName("KotlinUtil") annotation at the beginning of the file. Now we can call the addition function by using KotlinUtil.addition instead of CallKotlinUtilkt.addition. See the following example:
public static void main(String args[]) { KotlinUtil. ...Read now
Unlock full access