Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Kotlin Introduction

Kotlin Introduction

Kotlin is a statically typed language; types are checked at compile time, so many errors are caught before runtime.

It was first unveiled by JetBrains in 2011, released version 1.0 in 2016, and its name comes from Kotlin Island near Saint Petersburg, Russia.

Kotlin's original goal was to provide the JVM with a language more concise and safer than Java; today it can compile to multiple platforms.

The same .kt source file can be compiled to JVM bytecode, JavaScript, Wasm, or native machine code.

Note:Kotlin and Java have an interoperability relationship, not a replacement one. In the same project, both languages can coexist: Kotlin code can call Java libraries directly, and Java code can call Kotlin classes.


Kotlin's development history

Understanding the version timeline helps you judge which usages are outdated when reading older materials.

Year Version / Event Description
2011 Kotlin first unveiled JetBrains announced the project at the JVM Language Summit
2016 Kotlin 1.0 First official stable release
2017 Announced at Google I/O Kotlin becomes an officially supported Android development language
2019 Kotlin-first Google announces Kotlin as the preferred language for Android development
2020 Kotlin 1.4 Introduces SAM conversions, sealed interface, and trailing commas
2021 Kotlin 1.5 Value classes stabilized; JVM records support
2022 Kotlin 1.7 K2 compiler enters Alpha
2023 Kotlin 1.9 Range operator ..< and enum entries stabilized
2024 Kotlin 2.0 K2 compiler officially released, with significant improvements in compile speed and smart casts
2025 Kotlin 2.2 when guards stabilized; non-local break/continue stabilized

Kotlin 2.0 is a major turning point: the K2 compiler rewrote the front end, making compilation faster and type inference smarter.

All examples in this tutorial follow Kotlin 2.2; places involving version differences are explicitly marked "since X.Y".


Why choose Kotlin

Choosing a language is essentially choosing its ecosystem and the problems it helps you avoid.

Kotlin stands out especially in these areas.

Advantage Specific Manifestation
Conciseness One line of code can do work that takes many lines of boilerplate in Java, such as data classes and property accessors
Safe The type system distinguishes nullable from non-null types, keeping null pointer exceptions out at compile time
Interoperability Two-way calls with Java, so existing Java projects can migrate gradually without a rewrite
Multiplatform The same business logic can compile to JVM, JS, Wasm, iOS, Linux, and Windows
Tooling Native support in IntelliJ IDEA and Android Studio, with a mature official toolchain

If you already know Java, the cost of learning Kotlin lies mainly in functional style and null-safety rules; the syntax itself is not hard.


What Kotlin can do

Kotlin's compiler backends determine which platforms it can cover.

Currently four backends are officially supported, each corresponding to a class of use cases.

Backend Target platform Typical use
Kotlin/JVM Java Virtual Machine Android apps, Spring Boot servers, desktop tools
Kotlin/JS Browsers and Node.js Front-end pages, full-stack Kotlin projects
Kotlin/Wasm WebAssembly High-performance modules running in the browser
Kotlin/Native Native machine code Native programs and embedded on iOS, macOS, Linux, and Windows

Beyond that, Kotlin is also commonly used for data science (KotlinDL, DataFrame), scripting (.kts files), and Gradle build scripts.

Android development is currently the most widespread area for Kotlin, and most of Google's official new samples are written in Kotlin.


Your first Kotlin program

Start with the simplest form: one file, one entry function, one line of output.

The entry point of a Kotlin program is the main function; the file name can be anything, but lowercase names like hello.kt are customary.

Example

// File path: hello.kt

// First Kotlin program

fun main

(

)

{

    println

(

"Hello, Runoob!"

)

           

// Print a line of text and a newline

    println

(

"Welcome to www.runoob.com"

)

   

// Chinese text can also be printed directly

}

After compiling and running, the terminal outputs two lines.



Hello, Runoob!

欢迎来到 www.runoob.com

Kotlin also supports an object-oriented style, using classes to organize data and behavior.

The example below defines a class describing a website and calls its method.

Example

// File path: Site.kt

// Describe a website with a class; the primary constructor declares properties directly

class Site(val name: String, val url: String) {

    // Expression-body function; the return type is inferred as String by the compiler

    fun introduce(): String = "Welcome to $name ($url)"

}

fun main() {

    // Create an object; arguments are passed in order

    val runoob = Site("Runoob Tutorial", "www.runoob.com")

    println(runoob.introduce()) // Call the member function

    println("Site name length: ${runoob.name.length}") // Expressions can be placed in string templates

}

The result is as follows: "Runoob Tutorial" has 4 characters.



欢迎访问 Runoob Tutorial(www.runoob.com)

站点名称长度:4

Tip:The code above can be pasted directly into play.kotlinlang.org Run it online; no local environment installation is required.