Kotlin
In short
Kotlin is a concise, statically typed language that runs on the JVM, works seamlessly with Java, and is widely used for Android and server-side development.
What is Kotlin?
Kotlin is a general-purpose programming language developed by JetBrains and first released as version 1.0 in 2016. It was designed to address common pain points of Java while working seamlessly with it: Kotlin code can call Java libraries, and Java code can call Kotlin, often in the same project. In 2019, Google announced Kotlin as its preferred language for Android app development.
Kotlin is statically typed, with strong type inference, so val name = "Ada" is known to be a String without saying so. Types are non-nullable by default: a variable that may hold null must be declared with a question mark, such as String?, and the compiler forces you to handle the missing case. On the JVM, memory is managed by the JVM's garbage collector, and Kotlin can also compile to JavaScript, WebAssembly, and native code.
Kotlin is used for Android apps, server-side applications, and cross-platform projects that share business logic between Android, iOS, desktop, and web through Kotlin Multiplatform. Coroutines, a built-in way to write asynchronous code that reads like ordinary sequential code, make networking and background work simpler. Kotlin compared with Java is a bit like a newer car model that uses the same roads and fuel: it runs on the same JVM and libraries, but with extra safety features built in.
Kotlin is often seen as a replacement for Java, but it is better understood as a companion. It compiles to the same JVM bytecode, so teams commonly mix both languages in one codebase and migrate gradually. Typical differences in everyday code include null safety, data classes that remove boilerplate, and extension functions that add methods to existing types.
Key takeaways
- Kotlin compiles to JVM bytecode and interoperates fully with Java.
- Types are non-nullable by default, which helps prevent null pointer errors.
- It is statically typed with strong type inference and concise syntax.
- Coroutines provide lightweight asynchronous and concurrent programming.
- Kotlin can also target JavaScript, WebAssembly, and native code for multiplatform projects.
Example
// A data class gets equals, hashCode, toString and copy for free
data class User(val name: String, val email: String?)
fun main() {
val users = listOf(User("Ada", "ada@example.com"), User("Grace", null))
for (user in users) {
// ?. is a safe call and ?: supplies a default when the value is null
val domain = user.email?.substringAfter("@") ?: "no email"
println("${user.name}: $domain")
}
}Readers ask
What is the difference between Kotlin and Java?
Both compile to JVM bytecode and can be used together in the same project. Kotlin adds null safety in the type system, data classes, extension functions, and coroutines with more concise syntax, while Java has a longer history and a larger body of existing code.
Do I need to know Java to learn Kotlin?
No. Kotlin can be learned as a first language, although knowing Java helps when reading older Android code or using Java libraries.
Is Kotlin only for Android?
No. Kotlin is also used for server-side applications, and Kotlin Multiplatform can share code between Android, iOS, desktop, and web targets.
See also
- JavaProgramming Languages, p. 15Java is a statically typed, object-oriented programming language that compiles to bytecode for the Java Virtual Machine, so programs run on many platforms.
- SwiftProgramming Languages, p. 31Swift is a compiled, statically typed language designed for safety and speed, used mainly to build apps for iPhone, iPad, Mac, and other Apple platforms.
- Type InferenceProgramming Fundamentals, p. 55Type inference is a compiler feature that works out the type of a variable or expression automatically, so you don't have to write every type annotation.
- ConcurrencyProgramming Fundamentals, p. 12Concurrency is a program's ability to make progress on several tasks in overlapping time periods, such as serving many users at once rather than one at a time.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- OOPProgramming Fundamentals, p. 40OOP, or object-oriented programming, is a way of structuring code around objects that bundle related data together with the functions that act on that data.
- JVMProgramming Languages, p. 17The JVM runs Java bytecode, so the same compiled program works on any operating system; Kotlin, Scala and Clojure compile to that bytecode too.
- DartProgramming Languages, p. 8Dart is a statically typed, garbage-collected language from Google that compiles to native code and JavaScript, best known as the language of Flutter apps.
Spotted a mistake or something missing on this page?Suggest an edit