Can anyone tell me how is Kotlin interoperable wit...
# announcements
c
Can anyone tell me how is Kotlin interoperable with Java? Like I know it is, but how does that work?
d
Are you asking for how it works on a technical level in the compiler?
c
Yes @diesieben07. I understand the Kotlin code can be compiled down to Java ByteCode. But like how does that conversion work. If we a have a project with both Java and Kotlin, how do they interoperate with each other
z
What conversion?
c
Basically how does the interoperability works.
b
All jvm languages compile to jvm byte code. Since at runtime them all work on jvm bytecode level, source language doesn't matter anymore. So anything that compiles to jvm bytecode can automatically interop with all the other jvm languages. You can think of this process like typescript transpiling to js. End result is js with no trace of source input.
The difference here is that you obviously don't write jvm bytecode directly as opposed to being able to write plain js directly
In short, both java and kotlin sources end up in the same format at runtime, so technically they don't really interop anymore at that stage.
Interop here mainly refers to compiler frontend being aware of java classes pre-compillation
👍 1
e
there are certain Kotlin structures that don't translate very well into bytecode: default args result in secondary constructors or bridge methods with annotations, sealed interfaces aren't, etc.
but keep in mind that not everything in Java source has a representation in Java bytecode either (e.g. checked exceptions, multicatch/finally blocks, etc.)