Java InteroperabilityKotlin code is fully compatible with Java code. The existing Java code can be easily called form Kotlin code and Kotlin code is also called from Java code in normal way. Calling Java code from KotlinCalling Java void method form Kotlin fileWhile calling a java code from Kotlin whose return types is void, it will return Unit in Kotlin file. If someone wants to return that value, it will assign to Kotlin file by Kotlin compiler and return Unit. For example: MyKotlinFile.kt MyJavaClass.java Output: printing inside Java class :15 printing sum inside Kotlin file: kotlin.Unit Calling Java int method from Kotlin fileWhile calling a java code of int type or other (rather than void) from Kotlin file, it returns the result in same types. For example, calling an area() method of Java class from Kotlin file returns result in int type. MyKotlinFile.kt MyJavaClass.java Output: printing area from java insideKotlinfile: 12 Kotlin code calling Java class present inside packageIf we want to call the Java codes from Kotlin file both present inside the different package, this requires to import the package name with Java class inside Kotlin file. For example, a Java class MyJavaClass.java is present inside a package myjavapackageand a Kotlin file MyKotlinFile.kt is present inside mykotlinpackage package. In such case calling Java code from Kotlin file needs to import myjavapackage.MyJavaClass inside Kotlin file. MyKotlinFile.kt MyJavaClass.java Output: printing area from java inside Kotlin file: 12 Kotlin code access Java getter and setterAs Kotlin is completely interoperability with Java, we can access the getter and setter functionality of Java class (or POJO class). For example, create a getter and setter method in Java class MyJava.java with properties firstName and lastName. These properties are accessed from a Kotlin file MyKotlin.kt by creation object of MyJava.java in Kotlin file. MyJava.java MyKotlin.kt Output: accessing value using property: Arjun accessing value using property: Kumar accessing value using method: Arjun accessing value using method: Kumar Kotlin code access Java arrayWe can simply call Java class method which takes array as an argument from Kotlin file. For example, create method sumValue() which takes array element as parameter in Java class MyJava.java calculating addition and returns result. This method is called from Kotlin file MyKotlin.kt by passing array as parameter. MyJava.java MyKotlin.kt Output: sum of array element is 15 Kotlin code access Java VarargsIn the Java varags functionality, we can pass any number of arguments to a method. Java varargs parameter is defined using ellipsis i.e. three dots (...) after data type. Following points are to be kept while using the varargs parameter:
While accessing the Java varargs from Kotlin we need to use spread operator * to pass the array. Let's see an example in which a Java method uses an int type varargs which is called from Kotlin file. MyJava.java MyKotlin.kt Output: 0 1 2 3 Let's see another example which takes two parameter in a Java method uses as parameters of String type and int type varargs called from Kotlin file. MyJava.java MyKotlin.kt Output: string is hello 0 1 2 3 Kotlin and Java Mapped typesKotlin and Java types are mapped differently, however they are mapped to corresponding types. Mapping of these types are matters only at compile time and run time remains unchanged. Java's primitive types to corresponding Kotlin types
Java's non-primitive types to corresponding Kotlin types
Java's boxed primitive types to corresponding nullableKotlin types
Java's collection types to corresponding read-only or mutable Kotlin types
Next TopicCalling Kotlin from Java |