Kotlin
Kotlin 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 or native code via LLVM. Language development costs are borne by JetBrains, while the Kotlin Foundation protects the Kotlin trademark.
On 7 May 2019, Google announced that the Kotlin programming language had become its preferred language for Android app developers. 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, but allows targeting Java 9 up to 24, for optimizing, 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 has been declared stable since version 1.9.20, released November 2023.
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.Development
The first commit to the Kotlin Git repository was on 8 November 2010.In July 2011, JetBrains unveiled Project Kotlin, a new language for the JVM, which had been under development for a year. 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.
JetBrains expected Kotlin to drive IntelliJ IDEA sales.
Kotlin 1.0 was released on 15 February 2016. 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. 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.Semicolons are optional as a statement terminator; in most cases a newline is sufficient for the compiler to deduce that the statement has ended.
Kotlin variable declarations and parameter lists have the data type come after the variable name, 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.
The influence of Scala in Kotlin can be seen in the extensive support for both object-oriented and functional programming and in a number of specific features:
- there is a distinction between mutable and immutable variables
- all classes are public and final by default
- functions and methods support default arguments, variable-length argument lists and named arguments
Following , 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.
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 aJvmName annotation which specifies a class name used when the package is viewed from a Java project. For example, @file:JvmName.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. Perl, PHP, and Unix shell–style string interpolation is supported. Type inference is also supported.// Hello, World! example
fun main
fun main
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
>>> println)
By placing the preceding code in the top-level of a package, the String class is extended to include a function that was not included in the original definition of the String class.
// Overloading '+' operator using an extension function
operator fun Point.plus: Point
>>> val p1 = Point
>>> val p2 = Point
>>> println
Point
Scope functions
Kotlin has five scope functions, which allow the changing of scope within the context of an object. The scope functions are,,,, and.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
Destructuring declarations
Destructuring declarations decompose an object into multiple variables at once, e.g. a 2D coordinate object might be destructured into two integers, and.For example, the object supports destructuring to simplify access to its key and value fields:
for
println
Nested functions
Kotlin allows local functions to be declared inside of other functions or methods.class User
fun saveUserToDb
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
class GigaButton: MegaButton
Abstract classes are open by default
es 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
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:
| Keyword | Visibility |
public | Everywhere |
internal | Within a module |
protected | Within subclasses |
private | Within a class |
When applied to a top-level declaration:
| Keyword | Visibility |
public | Everywhere |
internal | Within a module |
private | Within a file |
Example:
// Class is visible only to current module
internal open class TalkativeButton
internal class MyTalkativeButton: TalkativeButton
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
//
open class BaseUser
open class PowerUser:BaseUser
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
//
class Context
class AttributeSet
open class View
class MyButton : View