Introduction to Kotlin

Explore the fundamentals of Kotlin, its versatile platforms, seamless integration with Kotlin IDE, and diverse applications across software development.

What is Kotlin?

Kotlin is an open-source, multiplatform, multiparadigm, statically typed, general-purpose programming language. But what does all this mean?

  • Open-source: The sources of the Kotlin compiler are freely available for modification and redistribution. Kotlin is primarily made by JetBrains, but now there is the Kotlin Foundation, which promotes and advances the development of this language. There is also a public process known as KEEP, which allows anyone to see and comment on official design change propositions.

  • Multiplatform: A language can be used on more than one platform. For instance, Kotlin can be used both on Android and iOS.

  • Multiparadigm: A language has support for more than one programming paradigm. Kotlin has powerful support for both object-oriented programming (OOP)OOP organizes code into objects with data and methods, promoting concepts like inheritance and encapsulation for modeling real-world relationships and interactions. and functional programming (FP)FP treats computation as mathematical function evaluations, prioritizing pure functions and immutability to enhance code predictability, maintainability, and parallelism..

  • Statically typed: Each variable, object, and function has an associated type that is known at compile-time.

  • General purpose: A language is designed to be used for building software in a wide variety of application domains across a multitude of hardware configurations and operating systems.

Note: These descriptions might not be clear now, but we will see them all in action throughout the course.

Kotlin platforms

Let’s start by discussing Kotlin’s multiplatform capabilities. Kotlin is a compiled programming language. This means that we can write some code in Kotlin and then use the Kotlin compiler to produce code in a lower-level language. Kotlin can currently be compiled into JVM bytecode (Kotlin/JVMKotlin is compiled for the JVM, allowing seamless interoperability with Java code and running on any platform with a JVM.), JavaScript (Kotlin/JSKotlin is compiled to JavaScript, enabling the use of Kotlin for front-end and web development.), or machine code (Kotlin/NativeKotlin is compiled directly to machine code, enabling high-performance applications on platforms where a virtual machine is not suitable, like embedded systems.).

Press + to interact
Compiling Kotlin as JVM bytecode, JavaScript, and machine code
Compiling Kotlin as JVM bytecode, JavaScript, and machine code

In this course, we would like to address all these compilation targets and, by default, show code that works on all of them, but we will concentrate on the most popular one: Kotlin/JVM.

Kotlin/JVM is the technology that’s used to compile Kotlin code into JVM bytecode. The result is nearly identical to the result of compiling Java code into JVM bytecode. We also use the term “Kotlin/JVM” to talk about code that will be compiled into JVM bytecode.

Press + to interact
Kotlin/Java bytecode compilation
Kotlin/Java bytecode compilation

Kotlin/JVM and Java Interoperability

Kotlin/JVM and Java are fully interoperable. Any code written in Java can be used in Kotlin/JVM. Any Java library, including those based on annotation processing, can be used in Kotlin/JVM. Kotlin/JVM can use Java classes, modules, libraries, and the Java standard library. Any Kotlin/JVM code can be used in Java (except for suspending functions, which are a support for Kotlin coroutines).

Press + to interact
Kotlin/JVM and Java: Interchangeable compatibility
Kotlin/JVM and Java: Interchangeable compatibility

Using Kotlin/JVM and Java together

Kotlin and Java can be mixed within a single project. A typical scenario is that a project is initially developed in Java, but then its creators decide to use Kotlin. To do this, instead of migrating the whole project, these developers decide to add Kotlin to it. So, whenever they add a new file, it’s a Kotlin file; furthermore, when they refactor old Java code, they migrate it to Kotlin. Over time, there is more and more Kotlin code until it excludes Java completely.

Press + to interact
Kotlin adoption in Java projects
Kotlin adoption in Java projects

One example of such a project is the Kotlin compiler itself. It was initially written in Java, but more and more files were migrated to Kotlin when it became stable enough. This process has been happening for years now; at the time of writing this course, the Kotlin compiler project still contains around 10% of Java code.

Misconceptions about Kotlin

Now that we understand the relationship between Kotlin and Java, it is time to fight some misconceptions. Many see Kotlin as a layer of syntactic sugar on top of Java, but this is not true. Kotlin is a different language than Java. It has its own conventions, practices, and features that Java does not have, such as coroutines.

We don’t need to know Java to understand Kotlin. Kotlin is a better first language than Java. Junior Kotlin developers don’t need to know what the equals method is and how to override it; for them, it’s enough to know the default class and data class equality. They don’t need to learn to write getters and setters or how to implement a singleton or a builder pattern either. Kotlin has a lower entry threshold than Java and does not need the JVM platform.

The Kotlin IDE

The most popular Kotlin (integrated development environments (IDEs) are:

  • IntelliJ IDEA

  • Android Studio

However, we can also write programs in Kotlin IDEs, including:

  • VS Code

  • Eclipse

  • Vim

  • Emacs

  • Sublime Text

Where do we use Kotlin?

Kotlin can be used as an alternative to Java, JavaScript, C++, Objective-C, etc. However, it is most mature on JVM, so it is currently used primarily as an alternative to Java.

  • Rise in back-end development: Kotlin has become quite popular for back-end development. We most often see it used with the Spring framework, but some projects use Kotlin with back-end frameworks like Vert.x, Ktor, Micronaut, http4k, or Javalin.

  • Dominance in Android development: Kotlin has also practically become the standard language for Android development. Google has officially suggested that all Android applications should be written in Kotlin and has announced that all their APIs will be designed primarily for Kotlin.

  • Cross-platform capabilities: More and more projects are now taking advantage of the fact that Kotlin can be compiled for a few different platforms because this means that teams can write code that runs on both Android and iOS or on both the backend and the frontend.

  • Multiplatform libraries and popularity: Moreover, this cross-platform compatibility means that library creators can create one library for multiple platforms at the same time. Kotlin’s multiplatform capabilities are already being used in many companies, and they are getting more and more popular.

  • Jetpack Compose for native UIs: It is also worth mentioning Jetpack Compose, which is a toolkit for building native UIs in Kotlin, was initially developed for Android, but it uses Kotlin’s multiplatform capabilities and can also be used to create views for websites, desktop applications, iOS applications, and other targets.

  • Front-end development with React: A lot of developers are using Kotlin for front-end development, mainly using React, and there is also a growing community of data scientists using Kotlin.

As we can see, there is already a lot that we can do in Kotlin, and there are more and more possibilities as each year passes.