I do have a little question about JVM and Kotlin. The JVM uses the Javac compiler to compile the Java code to bytecode. How does this work for Kotlin? Will the JVM use the Kotlinc compiler or will it still use the Javac compiler as Kotlin can be compiled to Java?
Asked
Active
Viewed 1,172 times
1 Answers
6
The JVM does not use the javac compiler. The process works differently: first, the developer runs javac to compile .java files to Java bytecode (.class files), and then the JVM loads the .class files and executes the bytecode.
To compile Kotlin code, you use the kotlinc compiler, which compiles .kt files to .class files. The JVM executes the bytecode from the .class files in exactly the same way; it does not care which compiler produced the .class files.
When compiling pure Kotlin projects, javac is not used in any way. When you compile a mixed-language project which contains both Java and Kotlin source files, the javac compiler is used to compile Java, and the kotlinc compiler is used to compile Kotlin.

yole
- 92,896
- 20
- 260
- 197
-
"When you compile a mixed-language project[..]the javac compiler is used to compile Java, and the kotlinc compiler is used to compile Kotlin." so for example on android studio it's gradle which pick the correct compiler, right? It's not kotlinc which calls javac for java files. Or it's different? – Dario Coletto Nov 29 '19 at 23:21
-
I assume the _javap_ command doesn't work then on _.class_ files which were compiled by kotlinc? – Physx Mar 17 '20 at 19:48
-
@Physx javap command work on any .class file. Including Kotlin code. – aldok May 02 '21 at 05:10