How does Kotlin interact with Java and JavaScript?

The Kotlin website says:

Kotlin is 100% Java compatible.

Is Kotlin a subset / superset of Java?

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

Is Java interpolating back to Kotlin?

+3


source to share


1 answer


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

This means that Kotlin has two compilers, the first is bytecode that can be run in the JVM, and the latter only produces Javascript.

This allows Kotlin to be compatible with both 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 naturally, and Kotlin code can also be used from Java.

Null-Safety and Platform Types

Any reference in Java can be null, which makes Kotlin's strict null safety requirements impractical for objects coming from Java. Java declaration types are handled specifically in Kotlin and are called platform types. Null checks are relaxed for such types, so the 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 generates Javascript, you can use Kotlin along with the Javascript source in the same JavaScript engine . This way you can call Kotlin from Javascript and / or call Javascript from Kotlin .

Calling JavaScript from Kotlin

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

Calling Kotlin from JavaScript

The Kotlin compiler generates regular JavaScript classes, functions, and properties that you can freely use from JavaScript code. however, there are some subtle things to remember. To prevent corruption of the global object, Kotlin creates an object containing all Kotlin declarations from the current module. Therefore, if you name your module as myModule

, all declarations are available to JavaScript through an object myModule

.

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

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

+8


source







All Articles