Java Basics: What Is Kotlin?
Kotlin is a statically typed programming language for JVMs, used to develop modern multiplatform applications.
In this blog, we take a look at Kotlin as a JVM language, discover how it's used, and conduct a side-by-side comparison of Kotlin vs. Java.
What Is Kotlin?
Kotlin is one of many Java Virtual Machine (JVM) languages and was developed by JetBrains, the maker of IntelliJ IDEA. Previously, developers struggled to update Java quickly as it was almost impossible to make bigger changes in syntax due to backward compatibility.
While the developers of JetBrains began Kotlin in 2010, it took over five years to release Kotlin version 1.0. One year later, in 2017, at the Google I/O conference, Kotlin was announced as an official language for Android development.
Nowadays, Kotlin is the second most used JVM language and is increasing in popularity due to the fact that it is developed under an open-source license and is easy to use. Developers also like Kotlin for its:
- Language features
- Structured concurrency
Kotlin vs. Java
Some may wonder why many Java developers are migrating to Kotlin. To find out why, let's compare Kotlin vs. Java using examples.
Kotlin vs. Java: Semicolons
In Kotlin, you no longer need to use a semicolon. It is only required if there are more commands on one line.
Kotlin vs. Java: Null Safety
In Kotlin, by default, you cannot assign a null value to any variable, unless you explicitly specify. This means all of them are non-nullable. If you try to do so, the compilation will fail. If for some reason you really need a null value, the variable must be declared as follow:
The only situation where you can see your favorite
NullPointerException is when you explicitly call it; you use the
!! operator when there is a data inconsistency.
Kotlin vs. Java: var vs. val
Although we now officially have
var in Java (although it still doesn't have
val), it didn't exist when Kotlin was released. When specifying variables, you can use keywords var or val. The keyword var is used for variables that can be changed during runtime, val represents the variable with the modification keyword final.
Kotlin vs. Java: Data Classes
Suppose you need a class to store data about something (e.g.,
Animal). In Java, you need to define everything – variables, constructors, methods such as
toString, etc. Kotlin uses
data to declare a class. The compiler will then take care of generating all necessary methods.
Kotlin for Android
Android supports Kotlin. In fact, at Google I/O 2019, Google announced the future of Android development would follow a Kotlin-first approach. Kotlin had been a developer favorite for a while and the demand for Kotlin support in Android was only increasing.
But what is a Kotlin-first approach? Don’t worry, Java is still supported; however, when new tools or content for Android development are made available, the design will be focused on Kotlin rather than Java. There will be also continuous support for APIs from Java.
The official IDE for Android app development, Android Studio, offers full support for Kotlin. Using a built-in tool, you can convert Java code to Kotlin. This can be helpful if you want to learn Kotlin on your existing project.
Get Started With Kotlin
It is easy to start working with Kotlin. The only two prerequisites are a JDK installed on your machine and a compatible IDE. At the time of writing, Kotlin requires JDK 6 or newer. Currently all major IDEs, such as IntelliJ, Android Studio, Eclipse, or Visual Studio Code, support Kotlin, though some require a plugin.
Kotlin Koans are available for those who want to get familiar with the Kotlin syntax. For those who do not want to install all the tools on their computer, Kotlin Playground is available use in a web browser.
All of this is just the tip of the iceberg where Kotlin is concerned. Many other things, such as lambda expressions, coroutines, and extensions would be enough not for just a blog post, but more likely for a book.
Kotlin is a good JVM language, easily adaptable by developers who use Java. You can start writing Java code and as you are exploring the language, you can transfer to Kotlin. It may easily become your number one language.
If you want to learn more about the top languages, IDEs, and application architectures Java developers use, download our new 2021 Java Developer Productivity Report.