liveBook · Manning

Static typing

As explained in Kotlin in Action, 2E

Static typing is a language discipline in which the type of every expression is determined at compile time.

Static typing determines every expression’s type at compile time. Kotlin’s compiler checks that accessed fields and methods exist and that function results have expected types, catching many errors before execution. section 1.2 section 1.2.2

val x: Int = 1    #1
val y = 1         #2

Kotlin often infers a variable’s type from its initializer, so explicit annotations and compile-time type information coexist. section 1.2.2

Static typing supports several practical benefits:

  • Performance: method calls do not require determining the target method at run time. section 1.2.2
  • Reliability: compiler checks reduce inconsistent operations and some run-time crashes. section 1.2.2
  • Maintainability and tool support: visible types assist unfamiliar-code navigation, refactoring, completion, and other IDE features. section 1.2.2

Static dispatch of extension functions

A variable declared as View can hold a Button, but extension resolution uses the variable’s declared static type: fun View.showOff() = println("I'm a view!") fun Button.showOff() = println("I'm a button!") val view: View = Button() view.showOff() // I'm a view! Regular overridden members dispatch differently: view.click() invokes Button.click(). Kotlin extensions compile to static Java functions, reinforcing that their receiver type is selected at compile time. section 3.3.4

Kotlin also uses its type system for safety. Nullable types are marked with ?, and the compiler rejects operations that could produce a NullPointerException. Type checks such as is allow safe member access after the check. section 1.4.3

How Kotlin in Action, 2E covers it

How deep it goes
Explains compiler checks, inferred declarations, static extension dispatch, nullability, and type checks, without detailing compiler internals.
Examples
Uses inferred integer variables, View and Button dispatch, nullable strings, and Any-to-String type checks.
What you can do after
Apply Kotlin’s type system through inferred declarations, nullable types, compiler-checked calls, and statically resolved extensions.
How it connects
Connects type inference, dynamic typing, extension functions, method overriding, null safety, and type checks.

Also explained in

Compare how 12 books explain static typing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.