How does Kotlin interoperate to Java and JavaScript?

lostAtSeaJoshua :

Kotlin website states:

Kotlin is 100% interoperable with Java.

Is Kotlin a subset/superset of Java?

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?

Does Java interpolate back to Kotlin?

freedev :

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

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

This lets Kotlin to be interoperable with both the languages.

About Kotlin and Java

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.

Calling Java code from Kotlin and Calling Kotlin from 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 and Platform Types

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

About Kotlin and Javascript

In the same way, when you use the Kotlin compiler that produces Javascript, you can have Kotlin running together with Javascript source in the same JavaScript engine. So you can call Kotlin from Javascript and/or call Javascript from Kotlin.

Calling JavaScript from 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.

Calling Kotlin from 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.

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.

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=476643&siteId=1