Kotlin | things about intermodulation with Java

Kt call - handling of non null Java parameters



class TestJava {
    public void toNotNull(@NotNull String title) {}

    public void toNull(String title){}

Call in Kotlin

KT call - variables and methods declared in Java using KT keyword

If you call the java method in kotlin, if you have a Kotlin keyword, you must add an anti quotation mark.


public Object object;
//Using keyword naming method in kotlin
public void is() {


Call in Kotlin


Kt calls Java Sam conversion

When you call a method with interface parameters in Kotlin, if this interface has only one method, you can implement SAM conversion through lambda expression, thus simplifying our code.

Examples are as follows


public class TestJavaSam {
    void singleFun(@NotNull IListener iListener) {}

    void noParameter( @NotNull IListener iListener,int sum) {}

    void noParameterClean(int sum,@NotNull IListener iListener) {}

interface IListener {
    void onClick();

Call in Kotlin

fun main() {
    val sam = TestJavaSam()
    sam.singleFun {

			//If you change the order of java method parameters, it will be more concise, as follows


Java is prohibited from calling a method in Kt


In Kotlin, some methods do not want to be exposed to Java calls, so you can add this annotation to the method.

fun toMain() {


At this point, toMain() cannot be called in Java.

Java calls Kt extension function


When using Kotlin's extension function in java, we will use the corresponding class name + Kt to call relevant methods. Sometimes it is a little troublesome for us to customize the corresponding tool class, as follows:

For example, we have a top-level extension function located in uiexpand KT:

fun Int.px() {}

Call in Java

  //Java calls the extension method of kotlin class - (UiExpand)-Int.px()

As shown above, when we call in Java, we must have the filename +kt suffix before it can be called.

By giving uiexpand By adding @ file:JvmName("Ui") to the package name of KT, we can realize the custom generated class name to call

as follows

Java call kt member variable


In Java, when we call the member variables of Kotlin, the compiler will automatically generate the corresponding get and set methods for us, which is very consistent with the writing method of Java Bean, but some of them are. We just want to call them directly, so we can do so at this time.


data class TestKotlinBean(
  @JvmField val message: String, 
  @JvmField val title: String)

Call in **Java

 TestKotlinBean testKotlinBean = new TestKotlinBean("","");
 String message = testKotlinBean.message;
 String title = testKotlinBean.title;

Of course, for the following examples, even without adding the above annotation, they can be called directly in java without getting and set.

lateinit var sum: String

object UserPicCache{
        const val KEY_CACHE = "CACHE"



Sometimes, we just want it to generate one of the set or get methods. What should we do at this time?


data class TestKotlinBean(
    var message: String,
    val title: String

Call in Java

 TestKotlinBean testKotlinBean = new TestKotlinBean("", "");

Kt companion object of Java call


When we call the method or variable of Kotlin associated object in Java, we must pass the class name. Companion.xx can be called. At this time, we can increase

@JvmStatic to call directly.


class Log {
    companion object {
        var time: String = ""
        fun toLog() {

Call in Java


However, it should be noted that @ JvmStatic does not improve the performance, because accordingly, the compiler generates another static method. For variable variables, it will generate two static methods, set and get.

Default parameter value of Kt method called by Java


In Kotlin, we may add some default values for method parameters for better use, but in Java, if the corresponding method parameters are not passed during the call, an error will be prompted. For this use, you can use @ JvmOverloads to modify the method.


object DialogUtils {
    fun showPromptDialog(title: String = "Tips") {

Call in java


See video:

Google developers - how to interoperate between Java and Kotlin

Added by inghamn on Sat, 12 Feb 2022 04:56:55 +0200