从Kotlin调用Java代码

As a matter of fact, Kotlin is an advanced programming language, which handles some modern features like Interoperability. In other words, two languages can communicate and exchange information with each other. As a result, calling Java codes from Kotlin would be a key factor in designing and implementing codes in Android development. This essay aims to consider some methods for calling Java codes from Kotlin in order to enhance reuseability and productivity of codes in Android.

实际上,Kotlin是一种高级编程语言,可处理诸如互操作性之类的一些现代功能。 换句话说,两种语言可以相互交流和交换信息。 结果,从Kotlin调用Java代码将成为在Android开发中设计和实现代码的关键因素。 本文旨在考虑从Kotlin调用Java代码的一些方法,以增强Android中代码的可重用性和生产率。

互操作性的定义是什么? (What is the definition of Interoperability?)

Interoperability means the ability of computer system or software to exchange and make use of information. The same concept is used for programming languages. It means the ability of two languages for communicating and exchanging information with each other such as functions and classes. Interoperability is a benefit because various programming languages are optimized for specific tasks, and allowing them to communicate for creating better software. As a matter of fact, it gives you code re-usability, which is a key factor for enhancing software productivity, and also reducing application development and maintenance costs.

互操作性是指计算机系统或软件交换和利用信息的能力。 相同的概念用于编程语言。 它意味着两种语言之间相互交流和交换信息的能力,例如功能和类。 互操作性是一个好处,因为针对特定任务优化了各种编程语言,并允许它们进行通信以创建更好的软件。 实际上,它为您提供了代码可重用性,这是提高软件生产率并降低应用程序开发和维护成本的关键因素。

Java和Kotlin之间的互操作性 (Interoperability between Java and Kotlin)

In fact, Kotlin code is fully compatible with Java code. Therefore, you can be able to use both the Java and Kotlin languages in a single project due to Interoperability. You can call Kotlin functions in Java as well as Java methods and variables in Kotlin code. For instance, if you have a Java project with classes and functions, then you do not need to rewrite the project in Kotlin from scratch. Alternatively, you can be able to use every line of Java code in Kotlin, and begin writing new functionalities here. Besides, you can call Kotlin codes in Java in the same way. There are some approaches to perform this issue as follows:

实际上,Kotlin代码与Java代码完全兼容。 因此,由于互操作性,您可以在单个项目中同时使用Java和Kotlin语言。 您可以在Java中调用Kotlin函数,也可以在Kotlin代码中调用Java方法和变量。 例如,如果您有一个带有类和函数的Java项目,则无需从头开始用Kotlin重写该项目。 另外,您可以使用Kotlin中的每一行Java代码,并在此处开始编写新功能。 此外,您可以以相同的方式用Java调用Kotlin代码。 有一些方法可以解决此问题,如下所示:

  1. Calling Java methods form Kotlin file

    从Kotlin文件中调用Java方法
  2. Calling the Java codes from Kotlin in different packages

    在不同的包中从Kotlin调用Java代码
  3. Accessing to Java array in Kotlin codes

    使用Kotlin代码访问Java数组
  4. Accessing to POJO Java class in Kotlin codes

    使用Kotlin代码访问POJO Java类
  5. Accessing Java Varargs in Kotlin codes

    使用Kotlin代码访问Java Varargs

1.从Kotlin文件中调用Java方法 (1. Calling Java methods form Kotlin file)

Fundamentally, calling the Java methods from the Kotlin codes is a straightforward concept. To accomplish this task, it must return the result in same types. The only exception to this rule is the void return type. Those functions in Java, which have a void return type, return a Unit type in Kotlin. Thus, this value can be saved in Kotlin as Unit as a type.

从根本上讲,从Kotlin代码调用Java方法是一个简单的概念。 要完成此任务,它必须返回相同类型的结果。 此规则的唯一例外是void返回类型。 Java中具有返回类型为void的那些函数在Kotlin中返回Unit类型。 因此,该值可以作为一种类型保存在Kotlin中。

// Kotlin codes 
fun main(args: Array<String>){ val rectangleArea: Int = SampleJavaClass.rectangleArea(10, 20)
println("inside the Kotlin codes:" + rectangleArea)
}//Java codes in SampleJavaClass.java
public class SampleJavaClass{
public static void main(String[] args){
} public static int rectangleArea(int x, int y){ int result = x * y;
return result;
}
}

If you want to call a java code from Kotlin, which return type is void, it will return Unit in Kotlin file. For instance, the output for these codes would be:

如果要从Kotlin调用Java代码(返回类型为void),它将在Kotlin文件中返回Unit。 例如,这些代码的输出为:

adding inside the Java : 170result inside the Kotlin: kotlin.Unit

在Java内部添加:170在Kotlin内部的结果:kotlin.Unit

//Kotlin codes in SampleKotlin.kt
fun main(args: Array<String>){ val result = SampleJavaClass(110, 60)
println ("sum inside the Kotlin:" + result)}//Java codes in SampleJavaClass.java
public class SampleJavaClass{ public static void main(String[] args){
} public static void add(int a, int b){ int adding = a + b;
System.out.println("adding inside the Java:" + adding); }
}

2.在不同的包中从Kotlin调用Java代码 (2. Calling the Java codes from Kotlin in different packages)

To call the Java codes from Kotlin file, which both of them are inside different packages, you should import the package name along with the Java class inside Kotlin file. For example:

要从Kotlin文件中调用Java代码,这两个代码都位于不同的包中,则应将包名称和Java类一起导入Kotlin文件中。 例如:

//Kotlin codes in SampleKotlin.ktpackage mykotlinpackage 
import mypackage.SampleJava //importing Java class here!
fun main(args: Array<String>){
val result= SampleJava.display()
println(result) }//Java codes in SampleJava.java
package mypackage;
public class SampleJava{
public static void main(String[] args){
}
public static void display(){ System.out.println("call the codes"); }
}

3.用Kotlin代码访问Java数组 (3. Accessing to Java array in Kotlin codes)

Basically, we can simply call Java class methods, which take array as an argument from Kotlin file. For example:

基本上,我们可以简单地调用Java类方法,该方法将数组作为Kotlin文件中的参数。 例如:

//Kotlin codes in SampleKotlin.kt
fun main(args: Array<String>){
val sampleObject = SampleClass()
val array = intArrayOf(2, 4, 6, 8, 10)
var sum = sampleObject.calculate(array)
println("The sum of the array is " + sum) //Output: 30}//Java Codes in SampleJava.java
public class SampleClass{ int result = 0;
public int calculate(int[] array){ for(int a: array){ result = result + a;
} return result;
}
}

4.用Kotlin代码访问POJO Java类 (4. Accessing to POJO Java class in Kotlin codes)

In general, the getters and setters of all the types, which are defined within the Java class are represented as properties in the Kotlin. Therefore, to access the getters and setters of a data member of a Java class, you must reference as a property within Kotlin. For instance:

通常,表示在Java类中定义的所有类型的getter和setter 作为Kotlin的财产。 因此,要访问Java类的数据成员的获取器和设置器,必须在Kotlin中作为属性引用。 例如:

//Kotlin codes in SampleKotlin.kt
fun main(args: Array<String>){val sampleObject = SampleJava()
sampleObject.setValue = 10 //Calling the setter function!
println(sampleObject.value) //Calling the getter function!}// Java class in SampleJava.java
public class SampleJava{ private int value; public int getValue(){ return value;
} public void setValue(int value){ this.value = value;
} }

5.用Kotlin代码访问Java Varargs (5. Accessing Java Varargs in Kotlin codes)

Principally, we can be able to pass any number of arguments to a method in the via Java Vargas functionality. Java Vargas parameter is defined using ellipsis i.e. three dots (…) after data type. For accessing the Java Vargas from Kotlin codes, you must use spread operator * to pass the array. For example:

原则上,我们可以通过Java Vargas功能将任意数量的参数传递给方法。 Java Vargas参数使用省略号定义,即数据类型后的三个点(…)。 要从Kotlin代码访问Java Vargas,必须使用扩展运算符*传递数组。 例如:

//Kotlin codes in SampleKotlin.kt
fun main(args: Array<String>){ val sampleJava = SampleJava()
val array = intArrayOf(2, 4, 6, 8)
sampleJava.display(*array) }//Java codes in SampleJava.java
public class SampleJava{
public void display(int... values){
for (int s : values){ System.out.println(s); }
}
}

映射类型 (Mapped types)

Basically, types in Kotlin are completely different from the types in Java, but to maintain interoperability Kotlin indicates mapping from Java types to Kotlin types. This mapping takes place at compile time, and there is no important change in performance at run-time. For instance:

基本上,Kotlin中的类型与Java中的类型完全不同,但是为了保持互操作性,Kotlin表示从Java类型到Kotlin类型的映射。 该映射在编译时发生,并且在运行时性能没有重要变化。 例如:

java.lang.Object — — — -> kotlin.Any!

java.lang.Object — — — —> kotlin。

java.lang.Long — — — -> kotlin.Long?

java.lang.Long — — —-> kotlin.Long?

long — — — -> kotlin.Long

长— — — —> kotlin.Long

In conclusion, Interoperability or calling codes from another language means two languages can communicate and exchange information with each other. So, calling Java codes from Kotlin would be a key factor in designing and implementing codes in Android development. This essay discussed five methods for implementing this issue.

总之,互操作性或来自另一种语言的调用代码意味着两种语言可以相互通信和交换信息。 因此,从Kotlin调用Java代码将是在Android开发中设计和实现代码的关键因素。 本文讨论了实现此问题的五种方法。

翻译自: https://medium.com/kayvan-kaseb/calling-java-codes-from-kotlin-b74890fb4a78

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值