Kotlin
General-purpose programming language
From Wikipedia, the free encyclopedia
Kotlin (/ˈkɒtlɪn/)[3] is a cross-platform, statically typed, general-purpose high-level programming language with type inference. Kotlin is designed to interoperate fully with Java, and the Java virtual machine (JVM) version of Kotlin's standard library depends on the Java Class Library. Type inference allows for more concise syntax than Java, while null-safety features reduce a common class of runtime errors. Kotlin mainly targets the JVM, but also compiles to JavaScript (e.g., for frontend web applications using React)[4] or native code via LLVM (e.g., for native iOS apps sharing business logic with Android apps).[5] JetBrains bears language development costs, while the Kotlin Foundation protects the Kotlin trademark.[6][7]
| Kotlin | |
|---|---|
| Paradigms | Multi-paradigm: object-oriented, functional, imperative, block structured, generic, reflective, concurrent |
| Family | JVM-hosted |
| Designed by | Andrey Breslav, JetBrains |
| Developer | JetBrains |
| First appeared | 22 July 2011 |
| Stable release | |
| Typing discipline | Inferred, static, strong |
| Platform |
|
| OS | Cross-platform |
| License | Apache 2.0 |
| Filename extensions | .kt, .kts, .kexe, .klib |
| Website | kotlinlang |
| Influenced by | |
| Influenced | |
| V (Vlang) | |
On 7 May 2019, Google announced Kotlin had become its preferred language for Android app developers.[8] As of 2024, over 95% of the top 1,000 Android apps on Google Play contain Kotlin code. In May 2024, JetBrains released Kotlin 2.0 featuring the new K2 compiler—a complete rewrite offering up to twice the compilation speed of its predecessor—as well as stabilised support for Kotlin Multiplatform.[9] That same month, Google formally endorsed Kotlin Multiplatform at Google I/O 2024, recommending it as the approach for sharing business logic across Android, iOS, and other platforms.[10]
History


Name
The name is derived from Kotlin Island, a Russian island in the Gulf of Finland, near Saint Petersburg. Andrey Breslav, Kotlin's former lead designer, mentioned that the team decided to name it after an island, in imitation of the Java programming language, which shares a name with the Indonesian island of Java.[11]
Development
The first commit to the Kotlin Git repository was on 8 November 2010.[12]
In July 2011, JetBrains unveiled Project Kotlin, a new JVM language that had been under development for a year.[13][14] JetBrains lead Dmitry Jemerov said that most languages lacked the features they were looking for, except for Scala. However, he cited Scala's slow compilation time as a deficiency.[14] One of Kotlin's stated goals is to compile as quickly as Java. In February 2012, JetBrains open-sourced the project under the Apache 2 license.[15]
Kotlin 1.0 was released on 15 February 2016, which is considered the first officially stable release, and JetBrains has committed to long-term backwards compatibility starting with this version.[16]
At Google I/O 2017, Google announced first-class support for Kotlin on Android.[6] On 7 May 2019, Google announced that Kotlin is now its preferred language for Android app developers.[8]
In November 2023, Kotlin/Native was declared stable with version 1.9.20, enabling production-ready compilation to native binaries for iOS, macOS, Linux, and Windows without a JVM.[17][18] Kotlin Multiplatform (KMP), which allows sharing business logic across Android, iOS, server, and desktop from a single codebase, also reached stable status in November 2023.[19][20][21]
In May 2024, JetBrains released Kotlin 2.0 at KotlinConf 2024 as the most significant release in the language's history. The centrepiece was the stable K2 compiler—a complete rewrite of the original compiler frontend from scratch—which unifies all target platforms (JVM, JavaScript, WebAssembly, and Native) into a single pipeline.[9] The K2 compiler was validated against 10 million lines of code across 80,000 projects by more than 18,000 developers before release.[9] Benchmarks showed compilation speed improvements of up to 94% in some projects, with the analysis phase up to 376% faster than the previous compiler.[22] Also in May 2024, Google officially endorsed Kotlin Multiplatform at Google I/O 2024, announcing first-class tooling and library support for KMP on Android, and revealing that Google Workspace uses KMP for the shared business logic of Google Docs across Android, iOS, and web.[23]
Design
Development lead Andrey Breslav has said that Kotlin is designed to be an industrial-strength object-oriented language, a "better language" than Java, and still fully interoperable with Java code, allowing companies to make a gradual migration from Java to Kotlin.[24]
Semicolons are optional as a statement terminator; in most cases, a newline is sufficient for the compiler to deduce that the statement has ended.[25]
Kotlin variable declarations and parameter lists place the data type after the variable name (separated by a colon), similar to Ada, BASIC, Pascal, TypeScript, and Rust. This, according to an article from Roman Elizarov, current project lead, results in alignment of variable names and is more pleasing to the eyes, especially when there are a few variable declarations in succession, and one or more of the types are too complex for type inference, or need to be declared explicitly for human readers to understand.[26][27]
The influence of Scala in Kotlin can be seen in the extensive support for both object-oriented and functional programming[28] and in several specific features:
- There is a distinction between mutable and immutable variables (var vs val keyword)
- All classes are public and final (non-inheritable) by default
- Functions and methods support default arguments, variable-length argument lists, and named arguments
Kotlin 1.3 added support for contracts,[29] which are stable for the standard library declarations, but still experimental for user-defined declarations. Contracts are inspired by the design-by-contract[30] programming paradigm.
Like Scala.js, Kotlin code can be transpiled to JavaScript, enabling interoperability between Kotlin and JavaScript and allowing either writing complete web applications in Kotlin or sharing code between a Kotlin backend and a JavaScript frontend.[31]
Syntax
Procedural programming style
Kotlin relaxes the Java restriction on static methods and variables, allowing them to exist outside a class body. Static objects and functions can be defined at the top level of the package without requiring a redundant class-level scope. For compatibility with Java, Kotlin provides the JvmName annotation, which specifies the class name used when the package is viewed from a Java project. For example, @file:JvmName("JavaClassName").
Main entry point
As in C, C++, C#, Java, and Go, the entry point to a Kotlin program is a function named main, which may be passed an array containing any command-line arguments. This is optional since Kotlin 1.3.[32] Perl, PHP, and Unix shell–style string interpolation is supported. Type inference is also supported.
// Hello, World! example
fun main() {
val scope = "World"
println("Hello, $scope!")
}
fun main(args: Array<String>) {
for (arg in args) {
println(arg)
}
}
Visibility modifiers
Kotlin provides the following keywords to restrict visibility for top-level declarations (such as classes) and for class members: public, internal, protected, and private.
When applied to a class member:
| Keyword | Visibility |
|---|---|
public (default) | Everywhere |
internal | Within a module |
protected | Within subclasses |
private | Within a class |
When applied to a top-level declaration:
| Keyword | Visibility |
|---|---|
public (default) | Everywhere |
internal | Within a module |
private | Within a file |
Example:
// Class is visible only to current module
internal open class TalkativeButton {
// method is only visible to current class
private fun yell() = println("Hey!")
// method is visible to current class and derived classes
protected fun whisper() = println("Let's talk!")
}
internal class MyTalkativeButton: TalkativeButton() {
fun utter() = super.whisper()
}
MyTalkativeButton().utter()
Null safety
Kotlin distinguishes between nullable and non-nullable data types. All nullable objects must be declared with a "?" postfix after the type name. Operations on nullable objects need special care from developers: a null-check must be performed before using the value, either explicitly, or with the aid of Kotlin's null-safe operators:
- ?. (the safe navigation operator) can be used to safely access a method or property of a possibly null object. If the object is null, the method will not be called, and the expression evaluates to null. Example:
// returns null if... // - foo() returns null, // - or if foo() is non-null, but bar() returns null, // - or if foo() and bar() are non-null, but baz() returns null. // vice versa, return value is non-null if and only if foo(), bar() and baz() are non-null foo()?.bar()?.baz()
- ?: (the null coalescing operator) is a binary operator that returns the first operand, if non-null, else the second operand. It is often referred to as the Elvis operator, due to its resemblance to an emoticon representation of Elvis Presley.
fun sayHello(maybe: String?, neverNull: Int) { // use of Elvis operator val name: String = maybe ?: "stranger" println("Hello $name") }
Lambdas
Kotlin supports higher-order functions and anonymous functions, or lambdas.[33]
Lambdas are declared using braces, { }. If a lambda takes parameters, they are declared within the braces and followed by the -> operator.
// the following statement defines a lambda that takes a single parameter and passes it to the println function
val l = { c : Any? -> println(c) }
// lambdas with no parameters may simply be defined using { }
val l2 = { print("no parameters") }
Tools
- Android Studio (based on IntelliJ IDEA) has official support for Kotlin since Android Studio 3.[34]
- Integration with common Java build tools is supported, including Apache Maven,[35][36] Apache Ant,[37][38] and Gradle.[39]
- Emacs has a Kotlin Mode in its MELPA package repository.[40]
- JetBrains also provides a plugin for Eclipse.[41]
- IntelliJ IDEA has plugin-based support for Kotlin.[42][43] IntelliJ IDEA 15 was the first version to bundle the Kotlin plugin in the IntelliJ Installer and to provide Kotlin support out of the box.[44]
- Kotlin integrates seamlessly with Gradle, a build automation tool.[45][46]
Kotlin Multiplatform
Kotlin Multiplatform enables a single codebase to target multiple platforms, including Windows, Linux, the web, Android, and iOS.[47][48]
KMP reached stable status in November 2023, and at Google I/O 2024, Google recommended it as the standard approach for sharing business logic across Android and iOS, also disclosing that Google Workspace uses KMP for the shared layer of Google Docs on Android, iOS, and web.[10]
Compose Multiplatform is an open-source, declarative framework for sharing UIs across multiple platforms – Android, iOS, web and desktop. It is based on Jetpack Compose and Kotlin Multiplatform.[49][50][51] Jetpack Compose uses a Kotlin compiler plugin to transform composable functions into UI elements.[52] For example, the Text composable function displays a text label on the screen.
Adoption
In 2018, Kotlin was the fastest-growing language on GitHub, with 2.6 times as many developers as in 2017.[53] It is the fourth-most-loved programming language according to the 2020 Stack Overflow Developer Survey.[54] In GitHub’s 2024 Octoverse report, Kotlin ranked fifth among the fastest-growing programming languages on the platform.[55]
Kotlin was also awarded the O'Reilly Open Source Software Conference Breakout Award for 2019.[56]
A large number of companies in various industries use Kotlin, including Google, Amazon Web Services, Booking.com and McDonald's.[57] Organisations using KMP in production include Forbes, Philips, McDonald's, and Square.[58]
According to the 2024 Stack Overflow Developer Survey, Kotlin ranked among the top admired programming languages, with approximately 57% of developers who used it in the prior year wishing to continue doing so.[59] The JetBrains Developer Ecosystem Survey 2024, conducted among more than 23,000 developers worldwide, found that Kotlin ranked among the highest-paid programming languages globally, alongside Scala, Go, and Rust.[60]
Applications
When Kotlin was announced as an official Android development language at Google I/O in May 2017, it became the third language fully supported for Android, after Java and C++.[61] Kotlin on Android is seen as beneficial for its null-safety and features that make code shorter and more readable.[62]
Ktor is a JetBrains Kotlin-first framework for building server and client applications.[63][64] The Spring Framework officially added Kotlin support with version 5, on 4 January 2017.[65] To further support Kotlin, Spring has translated all its documentation into Kotlin and added built-in support for many Kotlin-specific features, such as coroutines.[66]
In 2020, JetBrains found in a survey of developers who use Kotlin that 56% used it for mobile apps, while 47% used it for a web backend. Just over a third of all Kotlin developers said they were migrating from another language. Most Kotlin users targeted Android (or the JVM), with only 6% using Kotlin Native.[67]
As of 2024, over 95% of the top 1,000 Android apps on Google Play contain Kotlin code.[68]
By 2024, with Kotlin Multiplatform reaching stable status, that picture had shifted: a 2024 community survey of KMP developers found that nearly a quarter of respondents had adopted KMP within the prior six months, indicating strong recent growth, while a further 21% had used it for between one and three years. Google also reported at KotlinConf 2024 that it had migrated the shared business logic of Google Docs to KMP, running in production across Android, iOS, and the web.[69]