skip to content

Which packages are imported by default in every Kotlin file, and why does that matter for things like `println` or `listOf`?

level: juniorimportance: should knowfreq 45%

answer

  1. kotlin.* / collections / io / text default
  2. JVM also gets java.lang.* and kotlin.jvm.*
  3. println, listOf work import-free
  4. Defaults are platform-dependent
  5. kotlin.math is NOT default

basics

~10 s

Kotlin auto-imports some packages into every file, like kotlin., kotlin.collections., and kotlin.io.*. That's why you can call println, listOf, or use String without writing any import.

solid answer

~30 s

Every Kotlin file gets a set of **default imports** so common APIs need no explicit import. These include `kotlin.*`, `kotlin.collections.*`, `kotlin.io.*`, `kotlin.text.*`, `kotlin.ranges.*`, `kotlin.sequences.*`, `kotlin.comparisons.*`, and `kotlin.annotation.*`. On the JVM, `java.lang.*` and `kotlin.jvm.*` are also default. This is why `println` (kotlin.io), `listOf`/`mutableListOf` (kotlin.collections), `String`/`CharSequence` (kotlin via java.lang), and ranges (`1..10`) work without imports. The default set is **platform-dependent**: JS/Native add platform-specific defaults. It keeps idiomatic code terse. You only import when reaching outside these packages—e.g., `kotlin.math.sqrt` is NOT default and must be imported explicitly.

code

kotlin · 5 lines
kotlin
// No imports needed:
val m = mapOf("a" to 1)   // kotlin.collections
println(m)                  // kotlin.io
// Needs explicit import:
import kotlin.math.PI

go deeper

for a junior

Knows common stdlib like println/listOf needs no import due to default imports.

for a middle

Can name several default packages and that kotlin.math is not among them.

for a senior

Explains platform-specific default sets (JVM vs JS/Native) and the rationale.

for a principal

Considers implications for multiplatform code, expected/actual, and how default imports shape API discoverability and onboarding.

## What 'default imports' means Kotlin prepends an implicit set of imports to **every** source file so the standard library's most-used members are available with no `import` line. This is part of why Kotlin code looks concise. ## The common (all-platform) default packages - `kotlin.*` — core types and functions (e.g., `String`, `let`, `require`). - `kotlin.annotation.*` - `kotlin.collections.*` — `List`, `Map`, `listOf`, `mutableMapOf`, etc. - `kotlin.comparisons.*` - `kotlin.io.*` — `println`, `readLine`/`readln`. - `kotlin.ranges.*` — `IntRange`, the `..` machinery. - `kotlin.sequences.*` — `Sequence`, `generateSequence`. - `kotlin.text.*` — string/Char functions like `trim`, `Regex`. ## JVM-specific defaults - `java.lang.*` — so `Thread`, `Runnable`, `System`, etc., are available. - `kotlin.jvm.*` — JVM annotations like `@JvmStatic`, `@Throws`. ## Platform variation The defaults differ per target. Kotlin/JS and Kotlin/Native add their own (`kotlin.js.*`, etc.). So 'default imports' is a target-aware concept. ## Why it matters in practice ```kotlin fun main() { val xs = listOf(1, 2, 3) // kotlin.collections, no import println(xs.sum()) // kotlin.io + kotlin.collections for (i in 1..3) {} // kotlin.ranges } ``` None of these require imports. But: ```kotlin import kotlin.math.sqrt // kotlin.math is NOT a default import fun root(x: Double) = sqrt(x) ``` `kotlin.math.*` must be imported explicitly—a common surprise. ## Takeaway Know that stdlib essentials are auto-imported, that the set is platform-specific, and that some stdlib packages (notably `kotlin.math`, `kotlin.random`, `kotlin.system`) are **not** default and need explicit imports.

  • Name a stdlib package that is NOT imported by default.
    `kotlin.math` (also `kotlin.random`, `kotlin.system`, `kotlin.concurrent`) must be imported explicitly.
  • Are default imports the same on Kotlin/JS as on the JVM?
    No. The common kotlin.* packages are shared, but each platform adds target-specific defaults (e.g., java.lang.* on JVM, kotlin.js.* on JS).

Default imports are like kitchen staples already on the shelf; specialty ingredients (kotlin.math) you must fetch yourself.

saying these in an interview costs you the question

  • Claiming kotlin.math is auto-imported
  • Thinking java.lang.* is default on all platforms
  • Believing you must import listOf or println
  • Saying defaults are identical across all targets
  • Not knowing kotlin.collections is auto-imported

context