Kotlin (programming language)
{{Short description|General-purpose programming language derived from Java}}
{{Use dmy dates|date=July 2020}}
{{Infobox programming language
| name = Kotlin
| logo = Kotlin logo 2021.svg
| logo size = 230px
| paradigm = Multi-paradigm: object-oriented, functional, imperative, block structured, declarative, generic, reflective, concurrent
| family =
| released = {{Start date and age|2011|07|22|df=no}}
| designer = JetBrains
| developer = JetBrains
| latest release version = {{wikidata|property|reference|edit|P348}}
| latest release date = {{Start date and age|{{wikidata|qualifier|P348|P577}}|df=no}}
| latest preview version =
| latest preview date = {{Start date and age|2022|03|23|df=no}}
| typing = Inferred, static, strong
| implementations =
| dialects =
| platform = * Android
- JVM
- JavaScript ([https://kotlinlang.org/docs/js-overview.html Kotlin/JS])
- macOS (incl. Apple silicon support)
- iOS, tvOS, watchOS
- Linux
- Windows
- WebAssembly
- LLVM ([https://kotlinlang.org/docs/native-overview.html Kotlin/Native])
| operating system = Cross-platform
| license = Apache 2.0
| file_ext = .kt, .kts, .kexe, .klib
| website = {{url|https://kotlinlang.org/}}
| influenced by = {{Hlist|C#|Eiffel|Gosu|Groovy|Java|JavaScript|ML|Python|Scala}}
| influenced = V (Vlang)
}}
Kotlin ({{IPAc-en|ˈ|k|ɒ|t|l|ɪ|n}}){{cite web
|url=https://discuss.kotlinlang.org/t/what-is-the-correct-english-pronunciation-of-kotlin/2050
|title=What is the correct English pronunciation of Kotlin?
|date=16 October 2019
|access-date=9 November 2019
|archive-date=9 November 2019
|archive-url=https://web.archive.org/web/20191109155142/https://discuss.kotlinlang.org/t/what-is-the-correct-english-pronunciation-of-kotlin/2050
|url-status=live
}} 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 JVM version of Kotlin's standard library depends on the Java Class Library,
but type inference allows its syntax to be more concise. Kotlin mainly targets the JVM, but also compiles to JavaScript (e.g., for frontend web applications using React){{Cite web|title=Kotlin for JavaScript - Kotlin Programming Language|url=https://kotlinlang.org/docs/reference/js-overview.html|access-date=2020-08-20|website=Kotlin|language=en|archive-date=16 August 2020|archive-url=https://web.archive.org/web/20200816160848/https://kotlinlang.org/docs/reference/js-overview.html|url-status=live}} or native code via LLVM (e.g., for native iOS apps sharing business logic with Android apps).{{Cite web|title=Kotlin for cross-platform mobile development|url=https://www.jetbrains.com/lp/mobilecrossplatform/|access-date=2020-08-20|website=JetBrains: Developer Tools for Professionals and Teams|language=en|archive-date=19 August 2020|archive-url=https://web.archive.org/web/20200819163838/https://www.jetbrains.com/lp/mobilecrossplatform/|url-status=live}} Language development costs are borne by JetBrains, while the Kotlin Foundation protects the Kotlin trademark.{{Cite web|url=https://kotlinlang.org/foundation/kotlin-foundation.html|title=Kotlin Foundation - Kotlin Programming Language|website=Kotlin|access-date=16 December 2019|archive-date=29 December 2019|archive-url=https://web.archive.org/web/20191229152934/https://kotlinlang.org/foundation/kotlin-foundation.html|url-status=live}}
On 7 May 2019, Google announced that the Kotlin programming language had become its preferred language for Android app developers.{{cite web |last1=Lardinois |first1=Frederic |date=7 May 2019 |title=Kotlin is now Google's preferred language for Android app development |url=https://techcrunch.com/2019/05/07/kotlin-is-now-googles-preferred-language-for-android-app-development/ |access-date=8 May 2019 |website=TechCrunch |language=en-US |archive-date=7 May 2019 |archive-url=https://web.archive.org/web/20190507203145/https://techcrunch.com/2019/05/07/kotlin-is-now-googles-preferred-language-for-android-app-development/ |url-status=live}} Since the release of Android Studio 3.0 in October 2017, Kotlin has been included as an alternative to the standard Java compiler. The Android Kotlin compiler emits Java 8 bytecode by default (which runs in any later JVM), but allows targeting Java 9 up to 20, for optimizing,{{cite web |url=https://kotlinlang.org/docs/faq.html#which-versions-of-jvm-does-kotlin-target |title=Kotlin FAQ |quote=Kotlin lets you choose the version of JVM for execution. By default, the Kotlin/JVM compiler produces Java 8 compatible bytecode. If you want to make use of optimizations available in newer versions of Java, you can explicitly specify the target Java version from 9 to 21. Note that in this case the resulting bytecode might not run on lower versions. |access-date=2024-08-20 |archive-date=2 June 2021 |archive-url=https://web.archive.org/web/20210602213318/https://kotlinlang.org/docs/faq.html#which-versions-of-jvm-does-kotlin-target |url-status=live}} or allows for more features; has bidirectional record class interoperability support for JVM, introduced in Java 16, considered stable as of Kotlin 1.5.
Kotlin has support for the web with Kotlin/JS, through an intermediate representation-based backend which has been declared stable since version 1.8, released December 2022. Kotlin/Native (for e.g. Apple silicon support) has been declared stable since version 1.9.20, released November 2023.{{Cite web |title=Stability of Kotlin Components |url=https://kotlinlang.org/docs/components-stability.html |access-date=July 29, 2021 |date=May 21, 2021 |website=Kotlin |archive-date=29 July 2021 |archive-url=https://web.archive.org/web/20210729105608/https://kotlinlang.org/docs/components-stability.html |url-status=live}}{{Cite web |title=Kotlin 1.5.0 – the First Big Release of 2021 |url=https://blog.jetbrains.com/kotlin/2021/05/kotlin-1-5-0-released/ |access-date=July 29, 2021 |date=4 May 2021 |website=Kotlin |archive-date=12 August 2021 |archive-url=https://web.archive.org/web/20210812172858/https://blog.jetbrains.com/kotlin/2021/05/kotlin-1-5-0-released/ |url-status=live}}
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.{{Citation |last1=Mobius |title=Андрей Бреслав – Kotlin для Android: коротко и ясно |date=8 January 2015 |url=https://www.youtube.com/watch?v=VU_L2_XGQ9s |access-date=28 May 2017 |archive-date=12 April 2023 |archive-url=https://web.archive.org/web/20230412075657/https://www.youtube.com/watch?v=VU_L2_XGQ9s |url-status=live}}
= Development =
The first commit to the Kotlin Git repository was on November 8, 2010.{{Cite web |title=test · JetBrains/kotlin@3e4dce3 |url=https://github.com/JetBrains/kotlin/commit/3e4dce385331c91c9059fcdcea3eae2394f34942 |access-date=2022-10-17 |website=GitHub |language=en |archive-date=17 October 2022 |archive-url=https://web.archive.org/web/20221017184043/https://github.com/JetBrains/kotlin/commit/3e4dce385331c91c9059fcdcea3eae2394f34942 |url-status=live}}
In July 2011, JetBrains unveiled Project Kotlin, a new language for the JVM, which had been under development for a year.{{cite news |last1=Krill |first1=Paul |date=22 July 2011 |url=https://www.infoworld.com/d/application-development/jetbrains-readies-jvm-based-language-167875 |title=JetBrains readies JVM language Kotlin |work=InfoWorld |access-date=2 February 2014 |archive-url=https://web.archive.org/web/20190907161741/https://www.infoworld.com/article/2622405/jetbrains-readies-jvm-based-language.html |archive-date=7 September 2019 |url-status=live}} JetBrains lead Dmitry Jemerov said that most languages did not have the features they were looking for, with the exception of Scala. However, he cited the slow compilation time of Scala as a deficiency. One of the stated goals of Kotlin is to compile as quickly as Java. In February 2012, JetBrains open sourced the project under the Apache 2 license.{{cite news |last1=Waters |first1=John |date=22 February 2012 |url=https://adtmag.com/articles/2012/02/22/kotlin-goes-open-source.aspx |title=Kotlin Goes Open Source |website=ADTmag.com |publisher=1105 Enterprise Computing Group |url-status=live |archive-url=https://web.archive.org/web/20140218225151/https://adtmag.com/articles/2012/02/22/kotlin-goes-open-source.aspx |archive-date=18 February 2014 |access-date=2 February 2014}}
JetBrains expected Kotlin to drive IntelliJ IDEA sales.{{cite web |url=https://blog.jetbrains.com/kotlin/2011/08/why-jetbrains-needs-kotlin/ |title=Why JetBrains needs Kotlin |date=2 August 2011 |access-date=11 February 2018 |url-status=live |archive-url=https://web.archive.org/web/20230816024048/https://blog.jetbrains.com/kotlin/2011/08/why-jetbrains-needs-kotlin/ |archive-date=16 August 2023 |quote=we expect Kotlin to drive the sales of IntelliJ IDEA}}
Kotlin 1.0 was released on February 15, 2016.{{cite web |url=https://blog.jetbrains.com/kotlin/2016/02/kotlin-1-0-released-pragmatic-language-for-jvm-and-android/ |title=Kotlin 1.0 Released: Pragmatic Language for JVM and Android | Kotlin Blog |website=Blog.jetbrains.com |date=15 February 2016 |access-date=11 April 2017 |archive-date=24 January 2018 |archive-url=https://web.archive.org/web/20180124194203/https://blog.jetbrains.com/kotlin/2016/02/kotlin-1-0-released-pragmatic-language-for-jvm-and-android/ |url-status=live}} This is considered to be the first officially stable release and JetBrains has committed to long-term backwards compatibility starting with this version.
At Google I/O 2017, Google announced first-class support for Kotlin on Android.{{cite news |last1=Shafirov |first1=Maxim |date=17 May 2017 |url=https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/ |title=Kotlin on Android. Now official |quote=Today, at the Google I/O keynote, the Android team announced first-class support for Kotlin. |access-date=18 May 2017 |url-status=live |archive-url=https://web.archive.org/web/20230529180054/https://blog.jetbrains.com/kotlin/2017/05/kotlin-on-android-now-official/ |archive-date=29 May 2023}} On 7 May 2019, Google announced that the Kotlin programming language is now its preferred language for Android app developers.
Design
Development lead Andrey Breslav has said that Kotlin is designed to be an industrial-strength object-oriented language, and a "better language" than Java, but still be fully interoperable with Java code, allowing companies to make a gradual migration from Java to Kotlin.{{cite news |title=JVM Languages Report extended interview with Kotlin creator Andrey Breslav |url=https://zeroturnaround.com/rebellabs/jvm-languages-report-extended-interview-with-kotlin-creator-andrey-breslav/ |website=Zeroturnaround.com |date=22 April 2013 |access-date=2 February 2014 |archive-date=15 January 2019 |archive-url=https://web.archive.org/web/20190115182139/https://zeroturnaround.com/rebellabs/jvm-languages-report-extended-interview-with-kotlin-creator-andrey-breslav/ |url-status=live}}
Semicolons are optional as a statement terminator; in most cases a newline is sufficient for the compiler to deduce that the statement has ended.{{cite web |url=https://confluence.jetbrains.com/display/Kotlin/Grammar#Grammar-Semicolons |title=Semicolons |website=jetbrains.com |access-date=8 February 2014 |archive-date=23 December 2015 |archive-url=https://web.archive.org/web/20151223142747/https://confluence.jetbrains.com/display/Kotlin/Grammar#Grammar-Semicolons |url-status=live}}
Kotlin variable declarations and parameter lists have the data type come after the variable name (and with a colon separator), 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 eyes, especially when there are a few variable declarations in succession, and one or more of the types is too complex for type inference, or needs to be declared explicitly for human readers to understand.{{cite web|title=Types are moving to the right|url=https://elizarov.medium.com/types-are-moving-to-the-right-22c0ef31dd4a#:~:text=Woot!%20That%E2%80%99s%20nice%20and%20aligns,%20pleasure%20for%20our%20eyes%20to%20see.|website=Medium|date=16 July 2020|access-date=6 November 2021|archive-date=22 May 2023|archive-url=https://web.archive.org/web/20230522024640/https://elizarov.medium.com/types-are-moving-to-the-right-22c0ef31dd4a#:~:text=Woot!%20That%E2%80%99s%20nice%20and%20aligns,%20pleasure%20for%20our%20eyes%20to%20see.|url-status=live}}{{cite web|title=Roman Elizarov is the new Project Lead for Kotlin|url=https://blog.jetbrains.com/kotlin/2020/11/roman-elizarov-is-the-new-project-lead-for-kotlin/|website=The Kotlin Blog|date=19 November 2020|publisher=JetBrains|access-date=7 November 2021|archive-date=20 January 2022|archive-url=https://web.archive.org/web/20220120205530/https://blog.jetbrains.com/kotlin/2020/11/roman-elizarov-is-the-new-project-lead-for-kotlin/|url-status=live}}
The influence of Scala in Kotlin can be seen in the extensive support for both object-oriented and functional programming{{cite web |url=https://confluence.jetbrains.com/display/Kotlin/Functions |title=functions |website=jetbrains.com |access-date=8 February 2014 |archive-date=23 November 2015 |archive-url=https://web.archive.org/web/20151123112725/https://confluence.jetbrains.com/display/Kotlin/Functions |url-status=live}} and in a number of 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,{{Cite web|url=https://kotlinlang.org/docs/reference/whatsnew13.html|title=What's New in Kotlin 1.3 - Kotlin Programming Language|website=Kotlin|access-date=4 April 2020|archive-date=22 August 2023|archive-url=https://web.archive.org/web/20230822164600/https://kotlinlang.org/docs/reference/whatsnew13.html|url-status=live}} which are stable for the standard library declarations, but still experimental for user-defined declarations. Contracts are inspired by Eiffel's design by contract{{Cite web|url=https://discuss.kotlinlang.org/t/design-by-contract-dbc-design-considerations/1321|title=Design by Contract (DbC) design considerations|quote=Implement the full semantics of Eiffel DbC and improve upon it.|date=16 August 2012|website=Kotlin Discussions|language=en-US|access-date=4 April 2020|archive-date=5 April 2023|archive-url=https://web.archive.org/web/20230405081654/https://discuss.kotlinlang.org/t/design-by-contract-dbc-design-considerations/1321|url-status=live}} programming paradigm.
Following [https://www.scala-js.org/ ScalaJS], Kotlin code may be transpiled to JavaScript, allowing for interoperability between code written in the two languages. This can be used either to write full web applications in Kotlin, or to share code between a Kotlin backend and a JavaScript frontend.{{Cite web|title=Kotlin for JavaScript {{!}} Kotlin|url=https://kotlinlang.org/docs/js-overview.html#use-cases-for-kotlin-js|date=21 January 2021|access-date=2021-03-19|website=Kotlin Help|language=en-US|archive-date=14 July 2023|archive-url=https://web.archive.org/web/20230714143003/https://kotlinlang.org/docs/js-overview.html#use-cases-for-kotlin-js|url-status=live}}
Syntax
=Procedural programming style=
Kotlin relaxes Java's restriction of allowing static methods and variables to exist only within a class body. Static objects and functions can be defined at the top level of the package without needing a redundant class level. For compatibility with Java, Kotlin provides a JvmName
annotation which specifies a class name used when the package is viewed from a Java project. For example, @file:JvmName("JavaClassName")
.
=Main entry point=
{{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.{{cite web |url=https://play.kotlinlang.org/byExample/01_introduction/01_Hello%20world |title=Kotlin Examples: Learn Kotlin Programming By Example |access-date=13 April 2019 |archive-date=18 November 2021 |archive-url=https://web.archive.org/web/20211118223052/https://play.kotlinlang.org/byExample/01_introduction/01_Hello%20world |url-status=dead}} 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
for (arg in args)
println(arg)
}
=Extension functions=
Similar to C#, Kotlin allows adding an extension function to any class without the formalities of creating a derived class with new functions. An extension function has access to all the public interface of a class, which it can use to create a new function interface to a target class. An extension function will appear exactly like a function of the class and will be shown in code completion inspection of class functions. For example:
package com.example.myStringExtensions
fun String.lastChar(): Char = get(length - 1)
>>> println("Kotlin".lastChar())
By placing the preceding code in the top-level of a package, the String class is extended to include a {{code|lastChar}} function that was not included in the original definition of the String class.
// Overloading '+' operator using an extension function
operator fun Point.plus(other: Point): Point {
return Point(x + other.x, y + other.y)
}
>>> val p1 = Point(10, 20)
>>> val p2 = Point(30, 40)
>>> println(p1 + p2)
Point(x=40, y=60)
=Scope functions=
Kotlin has five scope functions, which allow the changing of scope within the context of an object. The scope functions are {{code|let}}, {{code|run}}, {{code|with}}, {{code|apply}}, and {{code|also}}.{{Cite web |title=Scope functions {{!}} Kotlin |url=https://kotlinlang.org/docs/scope-functions.html |access-date=2024-08-10 |website=Kotlin Help |language=en-US}}
=Unpack arguments with spread operator=
Similar to Python, the spread operator asterisk (*) unpacks an array's contents as individual arguments to a function, e.g.:
fun main(args: Array
val list = listOf("args: ", *args)
println(list)
}
=Destructuring declarations=
{{Distinguish|Destructor (computer programming)|text=the destructor method common in object-oriented languages}}
Destructuring declarations decompose an object into multiple variables at once, e.g. a 2D coordinate object might be destructured into two integers, {{Var|x}} and {{Var|y}}.
For example, the {{Code|code=Map.Entry}} object supports destructuring to simplify access to its key and value fields:
for ((key, value) in map)
println("$key: $value")
=Nested functions=
Kotlin allows local functions to be declared inside of other functions or methods.
class User(val id: Int, val name: String, val address: String)
fun saveUserToDb(user: User) {
fun validate(user: User, value: String, fieldName: String) {
require(value.isNotEmpty()) { "Can't save user ${user.id}: empty $fieldName" }
}
validate(user, user.name, "Name")
validate(user, user.address, "Address")
// Save user to the database
...
}
=Classes are final by default=
In Kotlin, to derive a new class from a base class type, the base class needs to be explicitly marked as "open". This is in contrast to most object-oriented languages such as Java where classes are open by default.
Example of a base class that is open to deriving a new subclass from it:
// open on the class means this class will allow derived classes
open class MegaButton {
// no-open on a function means that
// polymorphic behavior disabled if function overridden in derived class
fun disable() { ... }
// open on a function means that
// polymorphic behavior allowed if function is overridden in derived class
open fun animate() { ... }
}
class GigaButton: MegaButton() {
// Explicit use of override keyword required to override a function in derived class
override fun animate() { println("Giga Click!") }
}
=Abstract classes are open by default=
{{Main|Abstract class}}
Abstract classes define abstract or "pure virtual" placeholder functions that will be defined in a derived class. Abstract classes are open by default.
// No need for the open keyword here, it’s already open by default
abstract class Animated {
// This virtual function is already open by default as well
abstract fun animate()
open fun stopAnimating() { }
fun animateTwice() { }
}
=Classes are public by default=
Kotlin provides the following keywords to restrict visibility for top-level declaration, such as classes, and for class members: public
, internal
, protected
, and private
.
When applied to a class member:
class="wikitable"
! Keyword !! Visibility | |
public (default) | Everywhere |
internal | Within a module |
protected | Within subclasses |
private | Within a class |
When applied to a top-level declaration:
class="wikitable"
! 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()
=Primary constructor vs. secondary constructors=
Kotlin supports the specification of a "primary constructor" as part of the class definition itself, consisting of an argument list following the class name. This argument list supports an expanded syntax on Kotlin's standard function argument lists that enables declaration of class properties in the primary constructor, including visibility, extensibility, and mutability attributes. Additionally, when defining a subclass, properties in super-interfaces and super-classes can be overridden in the primary constructor.
// Example of class using primary constructor syntax
// (Only one constructor required for this class)
open class BaseUser(open var isSubscribed: Boolean)
open class PowerUser(protected val nickname: String, final override var isSubscribed: Boolean = true):BaseUser(isSubscribed) { }
However, in cases where more than one constructor is needed for a class, a more general constructor can be defined using secondary constructor syntax, which closely resembles the constructor syntax used in most object-oriented languages like C++, C#, and Java.
// Example of class using secondary constructor syntax
// (more than one constructor required for this class)
class Context
class AttributeSet
open class View(ctx:Context) {
constructor(ctx: Context, attr: AttributeSet): this(ctx)
}
class MyButton : View {
// Constructor #1
constructor(ctx: Context) : super(ctx) {
}
// Constructor #2
constructor(ctx: Context, attr: AttributeSet) : super(ctx, attr) {
// ...
}
}
=Sealed classes=
Sealed classes and interfaces restrict subclass hierarchies, meaning more control over the inheritance hierarchy.
Declaration of sealed interface and class:
sealed interface Expr
sealed class Job
All the subclasses of the sealed class are defined at compile time.
No new subclasses can be added to it after the compilation of the module having the sealed class.
For example, a sealed class in a compiled jar file cannot be subclassed.
sealed class Vehicle
data class Car(val brandName: String, val owner: String, val color: String): Vehicle()
class Bike(val brandName: String, val owner: String, val color: String): Vehicle()
class Tractor(val brandName: String, val owner: String, val color: String): Vehicle()
val kiaCar = Car("KIA", "John", "Blue")
val hyundaiCar = Car("Hyundai", "Britto", "Green")
=Data classes=
Kotlin's data class
construct defines classes whose primary purpose is storing data, similar to Java's record
types. Like Java's record
types, the construct is similar to normal classes except that the key methods equals
, hashCode
and toString
are automatically generated from the class properties. Unlike Java's records, data classes are open for inheritance.
=Kotlin interactive shell=
$ kotlinc-jvm
type :help for help; :quit for quit
>>> 2 + 2
4
>>> println("Hello, World!")
Hello, World!
=Kotlin as a scripting language=
Kotlin can also be used as a scripting language. A script is a Kotlin source file using the {{mono |.kts}} filename extension, with executable source code at the top-level scope:
// list_folders.kts
import java.io.File
val folders = File(args[0]).listFiles { file -> file.isDirectory() }
folders?.forEach(::println)
Scripts can be run by passing the -script
option and the corresponding script file to the compiler.
$ kotlinc -script list_folders.kts "path_to_folder_to_inspect"
=Null safety=
Kotlin makes a distinction 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:
- {{mono|?.}} (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:
{{blockquote |
// 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()
}}
- {{mono|?:}} (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.
{{blockquote |
fun sayHello(maybe: String?, neverNull: Int) {
// use of Elvis operator
val name: String = maybe ?: "stranger"
println("Hello $name")
}
}}
=Lambdas=
Kotlin provides support for higher-order functions and anonymous functions, or lambdas.{{cite web|title=Higher-Order Functions and Lambdas|url=https://kotlinlang.org/docs/reference/lambdas.html|website=Kotlin|publisher=Jetbrains|access-date=19 January 2018|archive-date=22 January 2021|archive-url=https://web.archive.org/web/20210122222128/https://kotlinlang.org/docs/reference/lambdas.html|url-status=live}}
// the following function takes a lambda, f, and executes f passing it the string "lambda"
// note that (String) -> Unit indicates a lambda with a String parameter and Unit return type
fun executeLambda(f: (String) -> Unit) {
f("lambda")
}
Lambdas are declared using braces, {{mono|{ }
// 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") }
="Hello world" example=
(Taken from and explained at https://kotlinlang.org/docs/kotlin-tour-hello-world.html.)
fun main() {
println("Hello, world!")
// Hello, world!
}
Tools
- Android Studio (based on IntelliJ IDEA) has official support for Kotlin, starting from Android Studio 3.{{Cite web|url=https://developer.android.com/kotlin|title=Kotlin and Android|website=Android Developers|access-date=19 June 2017|archive-date=4 October 2023|archive-url=https://web.archive.org/web/20231004062949/https://developer.android.com/kotlin|url-status=live}}
- Integration with common Java build tools is supported, including Apache Maven,{{cite web |url=https://kotlinlang.org/docs/reference/using-maven.html |title=Using Maven – Kotlin Programming Language |website=kotlinlang.org |access-date=9 May 2017 |archive-date=3 November 2016 |archive-url=https://web.archive.org/web/20161103233907/http://kotlinlang.org/docs/reference/using-maven.html |url-status=live}} Apache Ant,{{cite web |url=https://kotlinlang.org/docs/reference/using-ant.html |title=Using Ant – Kotlin Programming Language |website=kotlinlang.org |access-date=9 May 2017 |archive-date=3 November 2016 |archive-url=https://web.archive.org/web/20161103233835/http://kotlinlang.org/docs/reference/using-ant.html |url-status=live}} and Gradle.{{cite web |url=https://kotlinlang.org/docs/reference/using-gradle.html |title=Using Gradle – Kotlin Programming Language |website=kotlinlang.org |access-date=9 May 2017 |archive-date=9 November 2016 |archive-url=https://web.archive.org/web/20161109185109/http://kotlinlang.org/docs/reference/using-gradle.html |url-status=live}}
- Emacs has a Kotlin Mode in its MELPA package repository.
- JetBrains also provides a plugin for Eclipse.{{cite web |url=https://kotlinlang.org/docs/tutorials/getting-started-eclipse.html |title=Getting Started with Eclipse Neon – Kotlin Programming Language |website=Kotlinlang.org |date=10 November 2016 |access-date=11 April 2017 |archive-date=18 May 2023 |archive-url=https://web.archive.org/web/20230518022923/https://kotlinlang.org/docs/tutorials/getting-started-eclipse.html |url-status=live}}{{cite web |url=https://github.com/JetBrains/kotlin-eclipse |title=JetBrains/kotlin-eclipse: Kotlin Plugin for Eclipse |publisher=GitHub |access-date=11 April 2017 |archive-date=16 February 2016 |archive-url=https://web.archive.org/web/20160216150931/https://github.com/JetBrains/kotlin-eclipse |url-status=live}}
- IntelliJ IDEA has plug-in support for Kotlin.{{cite web |url=https://plugins.jetbrains.com/plugin/6954-kotlin |title=Kotlin :: JetBrains Plugin Repository |website=Plugins.jetbrains.com |date=31 March 2017 |access-date=11 April 2017 |archive-date=25 June 2023 |archive-url=https://web.archive.org/web/20230625051612/https://plugins.jetbrains.com/plugin/6954-kotlin |url-status=live}} 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.{{cite web |url=https://www.jetbrains.com/idea/whatsnew/ |title=What's New in IntelliJ IDEA 2017.1 |website=Jetbrains.com |access-date=11 April 2017 |archive-date=3 October 2023 |archive-url=https://web.archive.org/web/20231003212723/https://www.jetbrains.com/idea/whatsnew/ |url-status=live}}
- Gradle: Kotlin has seamless integration with Gradle, a build automation tool.{{Cite web |title=Gradle |url=https://kotlinlang.org/docs/gradle.html |access-date=2024-04-08 |website=Kotlin Help |language=en-US |archive-date=8 April 2024 |archive-url=https://web.archive.org/web/20240408064338/https://kotlinlang.org/docs/gradle.html |url-status=live}}
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++.{{Cite news |last1=Lardinois|first1=Frederic |url=https://techcrunch.com/2017/05/17/google-makes-kotlin-a-first-class-language-for-writing-android-apps/ |title=Google makes Kotlin a first-class language for writing Android apps |website=techcrunch.com |language=en-US |date=17 May 2017 |access-date=28 June 2018 |url-status=live |archive-url=https://web.archive.org/web/20170522065631/https://techcrunch.com/2017/05/17/google-makes-kotlin-a-first-class-language-for-writing-android-apps/ |archive-date=22 May 2017}} {{As of|2020}}, Kotlin was the most widely used language on Android, with Google estimating that 70% of the top 1,000 apps on the Play Store were written in Kotlin. Google itself had 60 apps written in Kotlin, including Maps and Drive. Many Android apps, such as Google Home, were in the process of being migrated to Kotlin, and therefore use both Kotlin and Java. Kotlin on Android is seen as beneficial for its null-pointer safety, as well as for its features that make for shorter, more readable code.{{cite web|url=https://www.zdnet.com/article/google-were-using-kotlin-programming-language-to-squash-the-bugs-that-cause-most-crashes/|website=ZDNet|title=Kotlin programming language: How Google is using it to squash the code bugs that cause most crashes|access-date=6 December 2020|archive-date=6 April 2023|archive-url=https://web.archive.org/web/20230406045458/https://www.zdnet.com/article/google-were-using-kotlin-programming-language-to-squash-the-bugs-that-cause-most-crashes/|url-status=live}}
In addition to its prominent use on Android, Kotlin was gaining traction in server-side development. The Spring Framework officially added Kotlin support with version 5, on 4 January 2017.{{cite web |url=https://spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring-framework-5-0 |website=Spring |title=Introducing Kotlin support in Spring Framework 5.0 |date=4 January 2017 |publisher=Pivotal |access-date=29 September 2020 |archive-date=23 August 2023 |archive-url=https://web.archive.org/web/20230823031409/https://spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring-framework-5-0/ |url-status=live}} To further support Kotlin, Spring has translated all its documentation to Kotlin, and added built-in support for many Kotlin-specific features such as coroutines.{{cite web|title=The State of Kotlin Support in Spring|url=https://blog.jetbrains.com/kotlin/2020/08/the-state-of-kotlin-support-in-spring/|website=JetBrains|date=14 August 2020|access-date=6 December 2020|language=en|archive-date=7 June 2023|archive-url=https://web.archive.org/web/20230607090327/https://blog.jetbrains.com/kotlin/2020/08/the-state-of-kotlin-support-in-spring/|url-status=live}} In addition to Spring, JetBrains has produced a Kotlin-first framework called Ktor for building web applications.{{cite web|url=https://dzone.com/articles/not-only-spring-boot-a-review-of-alternatives|website=DZone|title=Review of Microservices Frameworks: A Look at Spring Boot Alternatives|access-date=6 December 2020|archive-date=21 May 2023|archive-url=https://web.archive.org/web/20230521150446/https://dzone.com/articles/not-only-spring-boot-a-review-of-alternatives|url-status=live}}
In 2020, JetBrains found in a survey of developers who use Kotlin that 56% were using Kotlin for mobile apps, while 47% were using it for a web back-end. Just over a third of all Kotlin developers said that they were migrating to Kotlin from another language. Most Kotlin users were targeting Android (or otherwise on the JVM), with only 6% using Kotlin Native.{{cite web |title=Kotlin Programming - The State of Developer Ecosystem 2020 |url=https://www.jetbrains.com/lp/devecosystem-2020/kotlin/ |website=JetBrains |access-date=29 September 2020 |language=en |archive-date=5 April 2023 |archive-url=https://web.archive.org/web/20230405081654/https://www.jetbrains.com/lp/devecosystem-2020/kotlin/ |url-status=live}}
Adoption
In 2018, Kotlin was the fastest growing language on GitHub, with 2.6 times more developers compared to 2017.{{cite web |url=https://octoverse.github.com/projects |title=The state of the Octoverse |access-date=24 July 2019 |archive-url=https://web.archive.org/web/20190322190823/https://octoverse.github.com/projects |archive-date=22 March 2019 |url-status=dead}} It is the fourth most loved programming language according to the 2020 Stack Overflow Developer Survey.{{cite web|title=Stack Overflow Developer Survey 2020|url=https://insights.stackoverflow.com/survey/2020#most-loved-dreaded-and-wanted|access-date=28 May 2020|archive-date=4 June 2020|archive-url=https://web.archive.org/web/20200604090141/https://insights.stackoverflow.com/survey/2020#most-loved-dreaded-and-wanted|url-status=live}}
Kotlin was also awarded the O'Reilly Open Source Software Conference Breakout Award for 2019.{{cite web|url=https://blog.jetbrains.com/kotlin/2019/07/kotlin-wins-breakout-project-of-the-year-award-at-oscon-19/|title=Kotlin wins Breakout Project of the Year award at OSCON '19|date=18 July 2019|access-date=24 July 2019|archive-date=17 May 2022|archive-url=https://web.archive.org/web/20220517113227/https://blog.jetbrains.com/kotlin/2019/07/kotlin-wins-breakout-project-of-the-year-award-at-oscon-19/|url-status=live}}
Many companies/organizations have used Kotlin for backend development:
- Allegro{{cite web |title=Kotlin at Allegro |url=https://talkingkotlin.com/kotlin-at-allegro/ |website=Talking Kotlin |date=October 2018 |access-date=29 September 2020 |language=en |archive-date=1 June 2023 |archive-url=https://web.archive.org/web/20230601132552/https://talkingkotlin.com/kotlin-at-allegro/ |url-status=live}}
- Amazon{{cite web |title=QLDB at Amazon |url=https://talkingkotlin.com/qldb/ |website=Talking Kotlin |date=30 June 2020 |access-date=29 September 2020 |archive-date=1 June 2023 |archive-url=https://web.archive.org/web/20230601121344/https://talkingkotlin.com/qldb/ |url-status=live}}
- Atlassian{{cite web |title=Future of Jira Software powered by Kotlin |url=https://www.youtube.com/watch?v=4GkoB4hZUnw |website=YouTube | date=29 October 2019 |access-date=1 September 2023 |archive-date=1 September 2023 |archive-url=https://web.archive.org/web/20230901061928/https://www.youtube.com/watch?v=4GkoB4hZUnw |url-status=live}}
- Cash App{{cite web |title=Going Full Kotlin Multiplatform |url=https://talkingkotlin.com/going-full-kotlin-multiplatform/ |website=Talking Kotlin |date=14 April 2020 |access-date=29 September 2020 |language=en |archive-date=5 April 2023 |archive-url=https://web.archive.org/web/20230405221631/https://talkingkotlin.com/going-full-kotlin-multiplatform/ |url-status=live}}{{cite web |url=https://github.com/square/sqldelight |title=square/sqldelight |website=GitHub |access-date=24 July 2019 |archive-date=20 June 2019 |archive-url=https://web.archive.org/web/20190620112740/https://github.com/square/sqldelight |url-status=live}}
- Flux{{cite web |title=Using Kotlin for backend development at Flux |url=https://talkingkotlin.com/Using-Kotlin-for-backend-development-at-Flux/ |website=Talking Kotlin |date=14 August 2019 |access-date=29 September 2020 |language=en |archive-date=1 June 2023 |archive-url=https://web.archive.org/web/20230601111601/https://talkingkotlin.com/Using-Kotlin-for-backend-development-at-Flux/ |url-status=live}}
- Google{{cite web |title=State of Kotlin on Android |url=https://www.youtube.com/watch?v=AgPj1Q6D--c&feature=youtu.be&t=309 |access-date=29 September 2020 |website=YouTube | date=10 June 2020 |archive-date=15 January 2023 |archive-url=https://web.archive.org/web/20230115223201/https://www.youtube.com/watch?v=AgPj1Q6D--c&feature=youtu.be&t=309 |url-status=live}}
- Gradle{{cite web |title=Gradle Kotlin DSL Primer |url=https://docs.gradle.org/current/userguide/kotlin_dsl.html |website=docs.gradle.org |access-date=29 September 2020 |archive-date=18 August 2023 |archive-url=https://web.archive.org/web/20230818005932/https://docs.gradle.org/current/userguide/kotlin_dsl.html |url-status=live}}
- JetBrains{{cite web |title=Kotless |url=https://talkingkotlin.com/kotless/ |website=Talking Kotlin |date=30 January 2020 |access-date=29 September 2020 |language=en |archive-date=5 April 2023 |archive-url=https://web.archive.org/web/20230405081655/https://talkingkotlin.com/kotless/ |url-status=live}}
- Meshcloud{{cite web |title=Kotlin on the backend at Meshcloud |url=https://talkingkotlin.com/kotlin-on-the-backend-at-meshcloud/ |website=Talking Kotlin |date=28 February 2018 |access-date=29 September 2020 |language=en |archive-date=6 April 2023 |archive-url=https://web.archive.org/web/20230406111418/https://talkingkotlin.com/kotlin-on-the-backend-at-meshcloud/ |url-status=live}}
- Norwegian Tax Administration{{cite web |title=KotlinConf 2019: Kotlin Runs Taxes in Norway by Jarle Hansen & Anders Mikkelsen |url=https://www.youtube.com/watch?v=K8XxaAba65g&list=PLQ176FUIyIUY6SKGl3Cj9yeYibBuRr3Hl&index=22 |website=YouTube | date=16 December 2019 |access-date=29 September 2020 |archive-date=10 April 2023 |archive-url=https://web.archive.org/web/20230410210206/https://www.youtube.com/watch?v=K8XxaAba65g&list=PLQ176FUIyIUY6SKGl3Cj9yeYibBuRr3Hl&index=22 |url-status=live}}
- OLX{{cite web |title=Greenfield Kotlin at OLX |url=https://talkingkotlin.com/greenfield-kotlin-at-olx/ |website=Talking Kotlin |date=24 August 2018 |access-date=29 September 2020 |language=en |archive-date=1 June 2023 |archive-url=https://web.archive.org/web/20230601115345/https://talkingkotlin.com/greenfield-kotlin-at-olx/ |url-status=live}}
- Pivotal{{cite web |title=Application Monitoring with Micrometer |url=https://talkingkotlin.com/application-monitoring-with-micrometer/ |website=Talking Kotlin |date=14 June 2018 |access-date=29 September 2020 |language=en |archive-date=1 April 2023 |archive-url=https://web.archive.org/web/20230401140822/https://talkingkotlin.com/application-monitoring-with-micrometer/ |url-status=live}}
- Rocket Travel{{cite web |title=Groovy and Kotlin Interop at Rocket Travel |url=https://talkingkotlin.com/groovy-and-kotlin-interop-at-rocket-travel/ |website=Talking Kotlin |date=14 May 2018 |access-date=29 September 2020 |language=en |archive-date=5 April 2023 |archive-url=https://web.archive.org/web/20230405081653/https://talkingkotlin.com/groovy-and-kotlin-interop-at-rocket-travel/ |url-status=live}}
- Shazam{{cite web |title=Kotlin at Shazam |url=https://talkingkotlin.com/kotlin-at-shazam/ |website=Talking Kotlin |date=29 July 2018 |access-date=29 September 2020 |language=en |archive-date=1 April 2023 |archive-url=https://web.archive.org/web/20230401145508/https://talkingkotlin.com/kotlin-at-shazam/ |url-status=live}}
- Zalando{{cite web |title=Zally - An API Linter |url=https://talkingkotlin.com/Zally-An-API-Linter/ |website=Talking Kotlin |date=29 January 2018 |access-date=29 September 2020 |language=en |archive-date=6 April 2023 |archive-url=https://web.archive.org/web/20230406111421/https://talkingkotlin.com/Zally-An-API-Linter/ |url-status=live}}
Some companies/organizations have used Kotlin for web development:
- Barclay's Bank{{cite web |title=Java/Kotlin Developer - Barclays - Prague - Wizbii |url=https://www.wizbii.com/company/barclays/job/convertibles-trading-system-developer |website=Wizbii.com |access-date=29 September 2020 |language=en}}
- Data2viz{{cite web |title=KotlinConf 2017 - Frontend Kotlin from the Trenches by Gaetan Zoritchak |url=https://www.youtube.com/watch?v=1Pu0TYJJ2Tw&list=PLQ176FUIyIUY6UK1cgVsbdPYA3X5WLam5&index=14 |website=YouTube | date=30 November 2017 |access-date=29 September 2020 |archive-date=10 April 2023 |archive-url=https://web.archive.org/web/20230410010605/https://www.youtube.com/watch?v=1Pu0TYJJ2Tw&list=PLQ176FUIyIUY6UK1cgVsbdPYA3X5WLam5&index=14 |url-status=live}}
- Fritz2{{cite web |title=Fritz2 |url=https://talkingkotlin.com/fritz2/ |website=Talking Kotlin |date=14 September 2020 |access-date=29 September 2020 |language=en |archive-date=6 April 2023 |archive-url=https://web.archive.org/web/20230406204445/https://talkingkotlin.com/fritz2/ |url-status=live}}
- JetBrains{{cite web |title=KotlinConf 2019: Kotlin in Space by Maxim Mazin |url=https://www.youtube.com/watch?v=JnmHqKLgYY4 |website=YouTube | date=18 December 2019 |access-date=29 September 2020 |archive-date=9 April 2023 |archive-url=https://web.archive.org/web/20230409112530/https://www.youtube.com/watch?v=JnmHqKLgYY4 |url-status=live}}
A number of companies have publicly stated they were using Kotlin:
- Basecamp{{Cite news |url=https://m.signalvnoise.com/how-we-made-basecamp-3s-android-app-100-kotlin-35e4e1c0ef12 |archive-url=https://wayback.archive-it.org/all/20180801160548/https://m.signalvnoise.com/how-we-made-basecamp-3s-android-app-100-kotlin-35e4e1c0ef12?gi=e9a4b3c9bf9f |url-status=dead |archive-date=1 August 2018 |title=How we made Basecamp 3's Android app 100% Kotlin – Signal v. Noise |date=29 April 2017 |work=Signal v. Noise |access-date=1 May 2017}}
- Corda, a distributed ledger developed by a consortium of well-known banks (such as Goldman Sachs, Wells Fargo, J.P. Morgan, Deutsche Bank, UBS, HSBC, BNP Paribas, and Société Générale), has over 90% Kotlin code in its codebase.
- Coursera{{cite web |url=https://medium.com/coursera-engineering/becoming-bilingual-coursera-d8048dce73e3 |title=Becoming bilingual@coursera |date=26 April 2018 |access-date=24 July 2019 |archive-date=15 January 2023 |archive-url=https://web.archive.org/web/20230115223242/https://medium.com/coursera-engineering/becoming-bilingual-coursera-d8048dce73e3 |url-status=live}}
- DripStat{{cite web |url=https://blog.dripstat.com/kotlin-in-production-the-good-the-bad-and-the-ugly-2/ |title=Kotlin in Production – What works, Whats broken |website=Blog.dripstat.com |date=24 September 2016 |access-date=11 April 2017 |archive-date=1 July 2019 |archive-url=https://web.archive.org/web/20190701030659/https://blog.dripstat.com/kotlin-in-production-the-good-the-bad-and-the-ugly-2/ |url-status=dead}}
- Duolingo{{cite web |last1=Chaidarun |first1=Art |title=Migrating Duolingo's Android app to 100% Kotlin |url=https://blog.duolingo.com/migrating-duolingos-android-app-to-100-kotlin/ |website=Duolingo Blog |language=en |date=6 April 2020}}
- Meta{{cite web |url=https://www.infoq.com/news/2022/11/meta-port-java-kotlin/ |title=Porting Million Lines of Code from Java to Kotlin at Meta }}
- Netflix{{cite web |url=https://twitter.com/robspieldenner/status/708355228832178176 |title=Rob Spieldenner on twitter |access-date=24 July 2019 |archive-date=25 December 2022 |archive-url=https://web.archive.org/web/20221225132754/https://twitter.com/robspieldenner/status/708355228832178176 |url-status=live}}
- Pinterest{{cite web |url=https://www.youtube.com/watch?v=mDpnc45WwlI |title=Droidcon NYC 2016 - Kotlin in Production |website=YouTube |date=3 November 2016 |access-date=24 July 2019 |archive-date=14 April 2023 |archive-url=https://web.archive.org/web/20230414150938/https://www.youtube.com/watch?v=mDpnc45WwlI |url-status=live}}
- Trello{{cite web |url=https://twitter.com/danlew42/status/809065097339564032 |title=Dan Lew on Twitter |access-date=24 July 2019 |archive-date=2 May 2023 |archive-url=https://web.archive.org/web/20230502024553/https://twitter.com/danlew42/status/809065097339564032 |url-status=live}}
- Uber{{cite web |date=30 April 2019 |title=Measuring Kotlin Build Performance at Uber |url=https://www.uber.com/blog/measuring-kotlin-build-performance/ |access-date=6 March 2024 |archive-date=5 June 2023 |archive-url=https://web.archive.org/web/20230605173159/https://www.uber.com/blog/measuring-kotlin-build-performance/ |url-status=live}}
See also
{{Portal|Free and open-source software|Computer programming}}
References
- This article contains quotations from Kotlin tutorials which are released under an Apache 2.0 license.
{{Reflist}}
External links
- {{Official website}}
{{Java (software platform)}}
{{Programming languages}}
Category:Free software projects
Category:High-level programming languages
Category:Java programming language family
Category:JVM programming languages
Category:Object-oriented programming languages
Category:Programming languages
Category:Programming languages created in 2011