Kotlin
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Null Safety
-
Section 4: Object-Oriented Kotlin
-
Section 5: Functional Kotlin
-
Section 6: Coroutines
-
Section 7: Collections Deep Dive
-
Section 8: Type System Deep Dive
-
Section 9: Interop and Tooling
-
Section 10: Kotlin DSLs and Patterns
-
Section 11: Testing and Quality
-
Section 12: Server-Side Kotlin
-
Section 13: Practical Projects
-
Section 14: Interview Practice
-
Section 15: More Practice Exercises
-
Section 16: More Standard Library
-
Section 17: Multiplatform Kotlin
-
Section 18: kotlin.collections In Depth
-
Section 19: kotlin.text In Depth
-
Section 20: kotlin.ranges and kotlin.sequences
-
Section 21: kotlin.io and File Handling
-
Section 22: kotlinx.coroutines Deep Dive
-
Section 23: kotlin.reflect
-
Section 24: Android Development with Kotlin Overview
-
Section 25: Kotlin Multiplatform Deep Dive
-
Section 26: Kotlin for Backend Deep Dive
-
Section 27: Kotlin Design Patterns
-
Section 28: Advanced Language Features
-
Section 29: More Practice Exercises
-
Section 30: More Interview Practice
-
Section 31: Kotlin Type System Deep Dive
-
Section 32: Kotlin Null Safety Advanced
-
Section 33: Kotlin Testing Deep Dive
-
Section 34: Kotlin Build Tooling Deep Dive
-
Section 35: Kotlin Serialization
-
Section 36: Kotlin Performance Considerations
-
Section 37: Kotlin Native Overview
-
Section 38: Kotlin for Data and Scripting
-
Section 39: More Coroutines Practice
-
Section 40: More Android-Adjacent Patterns
-
Section 41: More Practical Projects
-
Section 42: More Design and Architecture Practice
-
Section 43: Kotlin Language Evolution
-
Section 44: More Interview and Review
-
Section 45: Kotlin Delegation Patterns Deep Dive
-
Section 46: Kotlin Annotations Deep Dive
-
Section 47: Kotlin for Gradle Plugin Development
-
Section 48: Kotlin Concurrency Beyond Coroutines
-
Section 49: Kotlin Compiler Internals
-
Section 50: Real-World Kotlin Case Studies
-
Section 51: Final Practice Projects
-
Section 52: Kotlin for Server-Side Reactive Programming
-
Section 53: More Practice and Drills
-
Section 54: Kotlin Security Practices
224: How Kotlin Compiles to JVM Bytecode
Wait, does Kotlin just turn into Java code first?
This is a common misconception. When you hit that "Build" button, Kotlin doesn't translate your code into a .java file and then call the Java compiler. That would be incredibly inefficient. Instead, the Kotlin compiler (kotlinc) translates your code directly into JVM bytecode—the same .class files that the Java compiler produces.
I like to think of bytecode as the "universal language" of the JVM. Whether you write in Kotlin, Java, or Scala, the JVM doesn't actually care about the source language. By the time the code reaches the Java Virtual Machine, it's all just a series of opcodes. This is why you can call a Kotlin function from a Java class without any friction; they're both speaking the same bytecode dialect under the hood.
How does the JVM handle things that don't exist in Java, like properties?
You've probably noticed that Kotlin lets you declare a property with a simple val or var. Java doesn't have "properties"—it has fields and methods. So, how does that work in bytecode? The Kotlin compiler performs a bit of "syntactic sugar" for you.
Take this simple class:
class User(val name: String)
If you peek at the bytecode, you'll see that the compiler actually generated a private field for the name and a public "getter" method. It looks roughly like this in Java-equivalent terms:
public final class User { private final String name; public User(String name) { this.name = name; } public final String getName() { return this.name; } }The JVM sees a field and a method, but you get to write a single line of Kotlin. This keeps the JVM happy while keeping your source code clean.
What happens to top-level functions since the JVM requires everything to be in a class?
This is one of my favorite "magic" tricks the Kotlin compiler pulls. In Kotlin, you can just drop a function into a file without wrapping it in a class. But the JVM is strict: every single method must belong to a class. So, how does Kotlin bypass this?
It creates a hidden "Facade" class. If you have a file named
MathUtils.ktwith a top-level function calledsquare(), the compiler generates a class calledMathUtilsKt(appending "Kt" to the filename).// In MathUtils.kt fun square(n: Int): Int = n * nUnder the hood, the bytecode creates something like this:
public final class MathUtilsKt { public static final int square(int n) { return n * n; } }When you call
square()in Kotlin, the compiler just directs the JVM to call the static method on that generated class. You never see the class, but it's there, doing the heavy lifting.How can I actually see this bytecode for myself?
You don't need to install some complex reverse-engineering tool to see this. If you're using IntelliJ IDEA, there's a built-in feature that makes this trivial. Go to
Tools > Kotlin > Show Kotlin Bytecode.A panel will open on the right showing the raw bytecode (which looks like assembly language and is, frankly, a headache to read). But here is the pro tip: click the "Decompile" button at the top of that panel. IntelliJ will attempt to turn that bytecode back into readable Java code. It won't be perfect, but it's the best way to understand exactly what the compiler is doing with your Kotlin shortcuts.
📋 Practical Task
Deconstructing a Data Class via Bytecode
In this exercise, you'll explore how Kotlin's data class is expanded during compilation. Follow these steps:
- Create a new Kotlin file and define a simple data class:
data class Product(val id: Int, val price: Double). - Use the Show Kotlin Bytecode tool in IntelliJ IDEA to open the bytecode panel.
- Click Decompile to view the generated Java source.
- Identify and list the four specific methods the Kotlin compiler automatically generated that you didn't explicitly write in your source code.
- Compare the
equals()method in the decompiled code to a manual implementation you might write in Java. Notice how the compiler handles the null checks and type comparisons for the properties.
There are no comments for now.