Kotlin如何与Java和JavaScript互操作? [英] How does Kotlin interoperate to Java and JavaScript?

查看:148
本文介绍了Kotlin如何与Java和JavaScript互操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kotlin网站声明:

Kotlin website states:


Kotlin与Java可100%互操作。

Kotlin is 100% interoperable with Java.

Kotlin是Java的子集/超集吗?

Is Kotlin a subset/superset of Java?

此外,文档还指出Kotlin与JavaScript兼容,那么如何编译它以支持两者? Kotlin是一种像Xamarin这样的跨平台语言吗?

Also the the documentation states that Kotlin is compatible with JavaScript so how is it compiled to support both? Is Kotlin a cross platform language like Xamarin?

Java是否插回到Kotlin?

Does Java interpolate back to Kotlin?

推荐答案


Kotlin是一种静态类型的编程语言,可以在
Java虚拟机上运行,​​也可以编译为JavaScript源
代码。

Kotlin is a statically-typed programming language that runs on the Java Virtual Machine and also can be compiled to JavaScript source code.

这意味着Kotlin有两个编译器,前者生成可以在JVM中执行的字节码,后者只生成Javascript。

This means that Kotlin has two compilers, the former produces bytecode that can be executed in the JVM and the latter produces just Javascript.

这使得Kotlin可以与这两种语言互操作。

This lets Kotlin to be interoperable with both the languages.

例如,当Java字节码和Kotlin字节码在同一个JVM中运行时,您可以从Kotlin调用Java 和/或从Java调用Kotlin

For example, when Java bytecode and Kotlin bytecode are running in the same JVM you can call Java from Kotlin and/or call Kotlin from Java.


从Kotlin调用Java代码从Java调用Kotlin

Calling Java code from Kotlin and Calling Kotlin from Java

Kotlin在设计时充分考虑了Java互操作性。现有的Java
代码可以通过Kotlin以自然的方式调用,而Kotlin代码也可以从Java中使用

Kotlin is designed with Java Interoperability in mind. Existing Java code can be called from Kotlin in a natural way, and Kotlin code can be used from Java rather smoothly as well.

Null-Safety和平台类型

Java中的任何引用都可能为null,这使得Kotlin的严格零安全要求
不切实际来自Java的对象。类型
的Java声明在Kotlin中专门处理,并称为
平台类型。对这些类型放宽空检查,因此安全
保证与Java相同

Any reference in Java may be null, which makes Kotlin's requirements of strict null-safety impractical for objects coming from Java. Types of Java declarations are treated specially in Kotlin and called platform types. Null-checks are relaxed for such types, so that safety guarantees for them are the same as in Java



关于Kotlin和Javascript



以同样的方式,当你使用产生Javascript的Kotlin编译器时,你可以让Kotlin与Javascript源一起运行在同一个 JavaScript引擎
所以你可以从Javascript调用Kotlin 和/或从Kotlin调用Javascript


从Kotlin调用JavaScript

Calling JavaScript from Kotlin

Kotlin旨在与Java平台轻松互操作。
将Java类视为Kotlin类,Java将Kotlin类视为
Java类。但是,JavaScript是一种动态类型语言,
,这意味着它不会在编译时检查类型。您可以通过动态
类型免费通过Kotlin与JavaScript进行
对话,但如果您想要Kotlin类型系统的全部功能,您可以
为JavaScript库创建Kotlin标头。

Kotlin was designed for easy interoperation with Java platform. It sees Java classes as Kotlin classes, and Java sees Kotlin classes as Java classes. However, JavaScript is a dynamically-typed language, which means it does not check types in compile-time. You can freely talk to JavaScript from Kotlin via dynamic types, but if you want the full power of Kotlin type system, you can create Kotlin headers for JavaScript libraries.

致电Kotlin JavaScript

Calling Kotlin from JavaScript

Kotlin编译器生成可以从JavaScript代码中自由使用的普通JavaScript类,函数和
属性。然而,
有一些你应该记住的微妙的东西。为了防止破坏全局>对象,Kotlin创建了一个对象,该对象包含来自当前模块的所有Kotlin声明
。因此,如果您将模块命名为 myModule ,则所有>声明都可通过 myModule 对象获得JavaScript

Kotlin compiler generates normal JavaScript classes, functions and properties you can freely use from JavaScript code. Nevertheless, there are some subtle things you should remember. To prevent spoiling the global > object, Kotlin creates an object that contains all Kotlin declarations from the current module. So if you name your module as myModule, all > declarations are available to JavaScript via myModule object.

关于你关于Xamarin的问题我只会说Xamarin和Kotlin是两个完全不同的东西。您可以将Xamarin与Ionic或PhoneGap进行比较,因为它们是可以让您创建多平台应用程序的产品。

Regarding your question about Xamarin I would only say that Xamarin and Kotlin are two completely different things. You can compare Xamarin with Ionic or PhoneGap, because they are products that lets you able to create a multi-platform app.

另一方面,Kotlin是一种语言,您可以编译该语言以在不同的环境和/或设备上运行您的程序。

On the other hand, Kotlin is a Language that you can compile to run your programs on different environments and/or devices.

这篇关于Kotlin如何与Java和JavaScript互操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆