blog heading image

Android App Development

Kotlin vs Java

When Android was first released, Java was the main programming language that supported development. After the release of Android Studio 3.0 in October 2017 however, the option to make a project using Kotlin was added as an alternative. Since May 2019, Kotlin has been the preferred language endorsed by Google for Android development. Kotlin can also be used for web and desktop development.

Kotlin, which many believe to be an improvement over Java, is still capable of being interoperable with Java code. It compiles to Java bytecode and runs on the Java Virtual Machine (JVM), which allows it to be used in areas where Java is normally used. This helps companies to gradually migrate to Kotlin. To make the process easier, both Android Studio and InteliJ have built-in functionality that automatically converts Java files to Kotlin. Kotlin is developed by JetBrains which had already developed many widely used tools, including IntelliJ and Resharper, while also being backed and adopted by Google. Kotlin is open source, which earns it backing from both JetBrains and the open-source community. Many believe that this means higher quality standards while also offering better support from its support team.

How Kotlin Compares to Java in Android App Development

A difference between Kotlin and Java is Kotlin’s lack of checked exceptions.

In Java, developers need to use try/catch blocks to catch exceptions when they call a method that could throw an exception. This can cause the error handling code to be mixed into the whole code base, even though it may be cleaner to handle these errors separately. This is not necessary in Kotlin.

Often when an exception occurs, a Java developer would need to write code to catch the exception, log the error, and exit the app. With Kotlin, this would happen without the developer needing to write specific code for it. Since Kotlin does not have the concept of a checked exception like Java, the developer is free to choose whether to handle an exception explicitly or let it bubble up and terminate the execution of the code at a higher level. If the developer thinks that an exception may occur and would like to handle this in a certain way, they can add a try/catch block around the method call, like they would do if the app was made with Java. However, unlike Java, this is not required by the Kotlin compiler.

Another benefit of Kotlin is code conciseness.

When comparing how many lines of code are needed between Kotlin and Java to do the same operation, Kotlin generally requires less code. For example, when an Android project that is written in Java needs to reference an object on the UI, it must first find the object by its ID. In Kotlin, a reference to the View would be imported into the Activity and the View would be treated as part of the Activity. This reduces the amount of code needed to find the UI objects the code needs to work with. The simplicity of the code also makes it easier to learn, especially for someone who previously worked with Java since it uses the concepts that a Java developer would already be familiar with.

The use of Coroutines in Kotlin is also another improvement over Java.

Since Android is single-threaded by default the UI will be blocked, and the app will freeze whenever there is a long-running operation to be completed. A way to avoid this in Java is to put these operations into background threads, but this can cause an increase in complexity and lead to debugging difficulties. Kotlin also allows additional threads to be created but also offers coroutines as an alternative. Coroutines use a lower amount of memory than threads. When performing long-running or intensive tasks, they are able to suspend and resume their execution without blocking the main thread and causing the app to freeze, while requiring more concise code than that required to create new threads.

Another feature that Kotlin offers is null safety.

In Java, variables can be set to have a value of null. However, if this value is used, it causes an exception that can cause the app to crash. By default, Kotlin does not allow a variable to be null unless specifically set to allow this. Kotlin also has syntax “safe calls” which allow functions to execute only when the variables required in it are not null.

Over the past few years, Kotlin has become one of the fastest-growing programming languages.

Business leaders like Pivotal, Atlassian, Pinterest, Evernote, and Uber are now using Kotlin for Android app development. According to AppBrain, as of December 2019, over 66,000 applications on the Google Play Store are using Kotlin, which makes up about 41% of the top apps. It is also well-liked as shown in a StackOverflow survey in which it was ranked the fourth most loved language. JetBrains has reported that Kotlin has seen high adoption rates since it was released and is expected to overtake Java for Android development. Several companies have stated publicly that they are now using Kotlin in their applications, including Netflix, Uber, and Pinterest.

Ever since it was released, Kotlin has had a positive impact in the industry. Many feel that Kotlin makes their code more readable and understandable. This makes the code easier to review and makes it easier to track down bugs. It creates greater ease in safely adding new functionality to an application which would affect the existing code. More concise code enables higher levels of productivity among developers because the effort needed to write the code is reduced. Less boilerplate code also means that developers are copying and pasting code from one place to another less often. Jetbrains reports that when converting Java code to Kotlin, it is expected that the lines of code are reduced by about 40%. Kotlin also allows for more code to be reused which is not possible when using Java.

Although Java will probably not be completely replaced anytime soon, Kotlin is now another option that has been shown to have a positive impact on the industry due to the improvements that it offers. Many companies now feel confident taking advantage of the benefits Kotlin offers and because it is supported by Google. The usage of Kotlin, especially in Android development, will likely grow in the future.

An example of an Android seed project which uses Kotlin can be found in this IPS Github repository.

By clicking “Accept”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. View our Privacy Policy for more information.