Reputation: 909
I am trying to understand if the compiled kotlin is a java bytecode file. In this example, I show the kt files and the class files, and there certain keywords that I don't know that existed in java. Eg, open val
.
Are the compiled kt files formed by bytecodes? Are the compiled kt files executed directly by the JVM?
Greeting.kt
package org.jetbrains.kotlin.demo
data class Greeting(val id: Long, val content: String)
GrettingController.kt
@RestController
class GreetingController {
val counter = AtomicLong()
@GetMapping("/greeting")
fun greeting(@RequestParam(value = "name", defaultValue = "World") name: String) =
Greeting(counter.incrementAndGet(), "Hello, $name")
}
Greeting.class
public final data class Greeting public constructor(id: kotlin.Long, content: kotlin.String) {
public final val content: kotlin.String /* compiled code */
public final val id: kotlin.Long /* compiled code */
public final operator fun component1(): kotlin.Long { /* compiled code */ }
public final operator fun component2(): kotlin.String { /* compiled code */ }
}
GreetingController.class
@org.springframework.web.bind.annotation.RestController public open class GreetingController public constructor() {
public open val counter: java.util.concurrent.atomic.AtomicLong /* compiled code */
@org.springframework.web.bind.annotation.GetMapping public open fun greeting(@org.springframework.web.bind.annotation.RequestParam name: kotlin.String): org.jetbrains.kotlin.demo.Greeting { /* compiled code */ }
}
Upvotes: 1
Views: 629
Reputation: 147921
Kotlin is not translated into Java and is instead directly compiled into the JVM bytecode (*.class
files). Some of the Kotlin language constructs actually cannot be expressed in Java.
The compiled code that you show is only represented as Kotlin pseudo-code for convenience of reading, and additional Kotlin metadata that is stored in the class files is used for that.
From the Kotlin FAQ:
What does Kotlin compile down to?
When targeting the JVM, Kotlin produces Java compatible bytecode. When targeting JavaScript, Kotlin transpiles to ES5.1 and generates code which is compatible with module systems including AMD and CommonJS. When targeting native, Kotlin will produce platform-specific code (via LLVM).
See also: how to view the bytecode when using IntelliJ IDEA.
Upvotes: 3