skip to content

What is the K2 compiler in Kotlin, and since which version is it the stable default?

level: juniorimportance: must knowfreq 55%

answer

  1. Frontend rewrite, not backend
  2. Stable default in Kotlin 2.0
  3. FIR = unified representation
  4. Faster + more consistent inference/smart casts
  5. Automatic — no flag on 2.0+

basics

~10 s

K2 is a rewritten Kotlin compiler frontend. It understands your code faster and more consistently. It became the stable default starting with Kotlin 2.0.

solid answer

~40 s

K2 is the new compiler frontend that JetBrains shipped as the stable default in Kotlin 2.0. The frontend is the part of the compiler that parses code, resolves names, runs type inference, and reports errors. K2 replaces the old frontend with a unified data structure called FIR (Frontend Intermediate Representation), giving faster compilation and more consistent type inference and smart casts. The backend (which generates JVM bytecode, JS, or native code via the IR backend) is largely shared. K2 is enabled automatically when your project uses Kotlin 2.0+; there is no flag to turn on. Before 2.0 you could opt in with languageVersion=2.0. Most user code compiles unchanged, though some previously-accepted-but-unsound programs are now correctly rejected.

go deeper

for a junior

Knows K2 is the new compiler and that 2.0 made it the default; can state it's automatic.

for a middle

Distinguishes frontend vs backend and names FIR as the unified representation.

for a senior

Explains why old programs may now be rejected (correctness) and that the backend is shared.

for a principal

Frames the rewrite as enabling future language features and tooling, weighing migration cost across a large codebase.

## What "K2" means A compiler has two halves. The **frontend** reads source text, resolves what every name refers to, runs **type inference** (figuring out types you didn't write), evaluates **smart casts**, and produces diagnostics (errors/warnings). The **backend** turns the analyzed program into output: JVM bytecode, JavaScript, or native binaries. **K2** is a full rewrite of the *frontend*. It shipped as the **stable default in Kotlin 2.0** (released May 2024). "K2" is the informal name; the headline language feature was numbered Kotlin **2.0**. ## Why rewrite the frontend? The old ("K1") frontend grew organically over a decade and used several internal representations that were hard to keep consistent. K2 introduces one unified structure — **FIR (Frontend Intermediate Representation)** — so all analysis phases work on the same model. Benefits: - **Faster** compilation (notably faster analysis on large projects). - **More consistent** type inference and **smart casts** — cases the old compiler handled inconsistently now behave uniformly. - A cleaner base for new language features and tooling. ## What stays the same The **IR-based backend** (bytecode/JS/native generation) is shared and was already in place before K2. So K2 is primarily a *frontend* story. ## How you turn it on You don't — it is automatic. ```kotlin // build.gradle.kts — using Kotlin 2.0+ means K2 is already the default plugins { kotlin("jvm") version "2.0.0" } ``` Before 2.0 you could preview it via the language version flag (`languageVersion = "2.0"`). On 2.0+ there is nothing to enable. ## Does my code break? Most code compiles unchanged. Because K2 is more correct, a few programs the old compiler *wrongly accepted* (unsound smart casts, ambiguous overloads) are now correctly **rejected** — these are real bugs surfaced, not regressions.

  • Which part of the compiler did K2 rewrite — frontend or backend?
    The frontend (parsing, name resolution, type inference, smart casts, diagnostics). The IR-based backend is largely shared.
  • Do you need a flag to enable K2 on Kotlin 2.1?
    No. It is the default on all 2.x versions; there is nothing to enable.

Like rebuilding a building's foundation while keeping the rooms (backend) mostly the same — sturdier and faster underneath.

saying these in an interview costs you the question

  • Saying K2 rewrote the bytecode generator / backend
  • Thinking K2 is an optional plugin you add
  • Claiming it became default in Kotlin 1.9
  • Believing all old code must be modified to compile
  • Confusing K2 with a new JVM target or runtime

context