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?
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.
Rather than directly generating JavaScript code from Kotlin source code, the Kotlin/JS IR compiler backend leverages a new approach. Kotlin source code is first transformed into a Kotlin intermediate representation (IR), which is subsequently compiled into JavaScript.
Kotlin is an Android-compatible language that is concise, expressive, and designed to be type- and null-safe. It works with the Java language seamlessly, so it makes it easy for developers who love the Java language to keep using it but also incrementally add Kotlin code and leverage Kotlin libraries.
Any chunk of code written in Kotlin is much smaller compared to Java, as it is less verbose and less code means fewer bugs. Kotlin compiles the code to a bytecode which can be executed in the JVM. Thus, all the libraries and frameworks made in Java transfer and run in a Kotlin project.
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.
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
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 viamyModule
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With