Purple and red gradient background with the Kotlin logo and Kotlin title.
January 21, 2021

Java Basics: What Is Kotlin?

Java Tools

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. 

Back to top

What Is Kotlin?

Kotlin is a statically typed programming language for JVMs, used to develop modern multiplatform applications.

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:

  • Expressiveness
  • Interoperability
  • Language features
  • Structured concurrency
Back to top

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.

Image of computer code with an example of Java code in Kotlin

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:

Image of computer code with var index: int? = null

The only situation where you can see your favoriteNullPointerException 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.

Computer code instructions demonstrating var and val in Kotlin

Kotlin vs. Java: Data Classes

Suppose you need a class to store data about something (e.g.,Person, Car, Animal). In Java, you need to define everything – variables, constructors, methods such as getters, setter, toString, etc. Kotlin uses data to declare a class. The compiler will then take care of generating all necessary methods.

Side-by-side comparison of code between Java and Kolin
Back to top

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. 

Back to top

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.

Back to top

Final Thoughts

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.

Additional Resources

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. 

Download Now 

 

Back to top