article banner (priority)

Kotlin in 3 minutes

This is a text-version of the video we published on YouTube.
Created as a better alternative to Java and later going far beyond that role, Kotlin is among the fastest-growing and most loved programming languages worldwide. In this video, I will give you an essential overview of Kotlin, starting from the top level and then diving into its code.
Kotlin can be compiled to JVM bytecode, just like Java. They both produce very similar outcomes, and they are fully interoperable. Kotlin can use all Java libraries, and can be used to provide a Java-friendly API. There are many mixed Kotlin and Java projects, with legacy Java parts that can easily work with Kotlin. Many modern Java libraries are written in Kotlin, and most Java developers who use them do not even know it.
However, Kotlin is a multiplatform language, and it can also be compiled to JavaScript or any native platform.
Thanks to that, Kotlin can be used not only on Android and backend, but also on iOS or websites, where it can run either on JavaScript or on Web Assembly.
This allows us to write code in Kotlin and release it so it can be used by multiple languages. This fact is used by more and more projects, defining common parts that are used both in Android and in iOS, or in backend and in frontend.
This capability is also used by some UI frameworks like Compose Multiplatform, which allows you to write code once and distribute it as native applications on Android, iOS, desktop, and web.
Maybe because of that, Kotlin is often described as a UI language, but it was initially designed for backend use. Even though practically all Android developers nowadays use Kotlin, there are more backend Kotlin developers than Android Kotlin developers. Kotlin is a first-class citizen in Spring Boot and has its own powerful frameworks, such as Ktor. Kotlin is now preferred over Java for backend services in many companies, including Google, Amazon, and Allegro. Now, without further ado, let’s dive into Kotlin code.
Kotlin is a static language with powerful type inference. Every variable is typed, but in most cases, this type can be inferred by the compiler.
It’s a strong null-safety mechanism that makes Kotlin programmers forget there is such a thing as a null-pointer exception. Regular types cannot be null; only types with a question mark are nullable, but such types cannot be used explicitly.
For handling nullable types, Kotlin offers tools such as safe calls and smart casts.
Smart casting is a tool that every language should have; it is compiler reasoning about types, so we don’t have to.
Kotlin supports named-optional arguments, like all other civilized languages.
fun sendEmail( email: String, subject: String = "No Subject", body: String = "No Body", attachments: List<File> = emptyList(), cc: List<String> = emptyList(), bcc: List<String> = emptyList(), priority: EmailPriority = EmailPriority.NORMAL ) { // ... } fun main() { sendEmail( email = "[email protected]", subject = "Hello", body = "This is a test email.", ) sendEmail( email = "[email protected]", subject = "Important Message", body = "This is an important message.", attachments = listOf(File("report.pdf")), priority = EmailPriority.HIGH ) }
In Kotlin constructor is defined after class name, and it can define properties. This way classes define dependencies first, and then their bodies.
It provides powerful support for immutability. Most variables are defined with val, which means they are read-only. Types such as List, Map, and Set are read-only. Data classes, which are a Kotlin way to represent a bundle of data, provide a copy method to create a copy with specific properties changed. All data classes in typical Kotlin projects are immutable, which gives us a degree of safety.
Kotlin syntax allows a simple definition of complex hierarchical structures. This feature is known as DSL, and it is now both a standard way to define UI with Jetpack Compose and a standard way to define build configurations in Gradle. DSL builders are used in many places, from request or response definitions in Ktor to implicit structured concurrency in Kotlin coroutines, a powerful Kotlin concurrency library.
That was Kotlin in 3 minutes. If you want to learn more about this language, I have published a series of five books that explain Kotlin from scratch through advanced features and best practices. I also conduct workshops for companies as a JetBrains partner. All details on kt.academy website.