skip to content

Are semicolons required in Kotlin? When, if ever, do you actually need one?

level: juniorimportance: must knowfreq 55%

answer

  1. Newline = implicit statement end
  2. Explicit ; only for two statements on one line
  3. enum: ; separates constants from members
  4. Trailing ; is redundant (ktlint/detekt flag it)
  5. Break long lines AFTER the operator/dot

basics

~20 s

No. Kotlin ends statements at line breaks, so you almost never write a semicolon. You only need one to put two statements on the same line, like val a = 1; val b = 2.

solid answer

~30 s

Semicolons are optional in Kotlin; the compiler infers statement ends from newlines, so idiomatic code omits them. You need an explicit `;` only when you put two or more statements on one line, e.g. `val x = 1; println(x)`. There is also one mandatory case: separating an `enum` constant list from member declarations inside an enum class body. Trailing semicolons are allowed but flagged by ktlint/detekt as redundant. Because end-of-line ends a statement, formatting matters: a binary operator or `.` left at line start can change parsing, so dangling operators usually go at the end of the previous line.

code

kotlin · 12 lines
kotlin
// Idiomatic: no semicolons
val name = "Kotlin"
println(name)

// Required: two statements, one line
val a = 1; val b = 2

// Required: enum constants vs members
enum class Suit {
    HEARTS, SPADES;
    fun red() = this == HEARTS
}

go deeper

for a junior

Knows semicolons are optional and rarely written; can give the one-line-two-statements case.

for a middle

Also recalls the mandatory enum constants/members separator and that ktlint flags redundant semicolons.

for a senior

Explains how newline-based termination interacts with line breaking and operator placement; cites the leading-+ pitfall.

for a principal

Frames it as a lexer/parser design choice (implicit semicolon insertion), discusses tooling enforcement and team formatting conventions to avoid ambiguity.

## The rule Kotlin uses **newline-based statement termination**. The lexer treats the end of a line as the end of a statement when the line forms a complete statement. Therefore a trailing semicolon is **optional and idiomatically omitted**. ## When you MUST use a semicolon There are only two situations: 1. **Multiple statements on one line** — the semicolon is the separator: ```kotlin val a = 1; val b = 2; println(a + b) ``` 2. **Inside an `enum class`**, to separate the comma-list of constants from any further members (methods, properties): ```kotlin enum class Color { RED, GREEN, BLUE; // <- required semicolon fun lower() = name.lowercase() } ``` Without that `;` the compiler can't tell where the constant list ends. ## When a semicolon is harmless but discouraged A trailing `;` at the end of a line compiles fine but is **redundant**; `ktlint` and `detekt` report it (`no-semi` style). Keep code clean by omitting it. ## Why line breaks matter (the subtle part) Because a newline can terminate a statement, where you break a long expression affects parsing. Leading-operator style can silently change meaning: ```kotlin val a = 1 + 2 // parsed as a unary +2 expression statement, NOT 1 + 2! ``` The `val a` is assigned `1`; the `+ 2` becomes a separate (useless) expression. To continue an expression, leave the operator or `.` at the **end** of the previous line: ```kotlin val a = 1 + 2 // correct: 3 val s = builder .append("x") .toString() ``` ## Mental model Think "newline = implicit semicolon, unless the line is obviously incomplete (ends with `+`, `.`, `(`, `,`, `=`, etc.)." Reach for an explicit `;` only to cram statements together or to close an enum constant list.

  • Show a case where omitting a semicolon changes the meaning of code.
    Breaking before a binary operator: `val a = 1` then a new line `+ 2` makes `+2` a separate unary expression, so `a` is 1, not 3. Keep the operator at the end of the previous line.
  • Is the semicolon after enum constants always required?
    Only when the enum body has further members (functions/properties) after the constant list. If the enum has only constants, no semicolon is needed.

Like spoken sentences ending on a pause — you only add a written period when you cram two thoughts into one breath.

saying these in an interview costs you the question

  • Claiming Kotlin always requires semicolons like Java
  • Adding a trailing `;` to every line
  • Not knowing the enum constant-to-member separator case
  • Breaking long expressions before an operator and assuming it still works
  • Saying semicolons are forbidden (they're optional, not banned)

context