651.288.7000 info@intertech.com

Kotlin, Explained: The Java Alternative & Why Android Is Supporting It

by | Oct 8, 2018

Recently, a new programming language, Kotlin, has exploded onto the scene. Over the course of the past two years, Kotlin has gone from relative obscurity to being recognized by Google as a supported language for Android development. Kotlin’s rise has been driven by its strong features, concise syntax, and interoperability with Java.

Painted in broad strokes, Kotlin is a general purpose, open source programming language. It’s statically typed, but its compiler can infer typing for most variables without the need to declare them. This feature, along with other simplifications, makes Kotlin essentially a cleaner version of Java, removing a lot of boilerplate syntax.

Kotlin compiles to the same bytecode as Java. It also runs the same compile-time checks as Java does, along with a few additional checks. As such, it runs seamlessly on the Java Virtual Machine (JVM) and Android without the need for bridges or wrappers. Kotlin’s community and developers have also implemented a complete suite of language features and library functions that make Kotlin an effective language for both object-oriented and functional programming in nearly any context where you might use Java.

 

History of Kotlin

Kotlin comes from JetBrains, the Czech software shop famous for their IntelliJ Integrated Development Environment (IDE). They initially developed Kotlin out of a desire to clean up Java’s syntax while still maintaining full interoperability with existing Java code.

The project began in 2011, and in 2012 JetBrains released the first version of the Kotlin language as open source. This initial release gained a small following of diehard fans that supported the project’s goals of making Java more concise and secure. However, Kotlin remained a niche project until its 2016 release of version 1.1.

Version 1.1 added more language features, library functions, and support that made the language usable for complex, enterprise-level applications and mobile apps. As Kotlin’s popularity grew, developers started to take notice, including the Android team at Google. In 2017, at Google’s I/O Conference, the Android team announced that Kotlin would join Java as a “first-class” language for Android development.

Of course, it helps that Android Studio – Google’s official developer tool – runs a version of JetBrains’ IDE. Updating Android Studio for Kotlin support was easy with the IDE developer and the language developer being the same company. After the announcement, interest in Kotlin spiked. By the end of 2017, 10 million lines of code on GitHub had already been written in Kotlin with contributions from over 160,000 users.

 

Concise, Readable, Secure

Reading Kotlin source code, it looks like a streamlined version of Java. Kotlin’s main goal was removing boilerplate syntax that goes into class, method, and variable declaration and use. It also generally makes the code look cleaner by removing the need for semicolons at the end of a line, for instance, or reducing restatements. This succinctness means you get more power per keystroke in Kotlin without sacrificing comprehension.

For example, defining a function only requires an invocation of the fun keyword. The val keyword declares a read-only property while the var keyword declares a property that can change. However, Kotlin is still strongly typed. You can only use val and var when the compiler can infer the variable type.

Structurally, Kotlin is simpler, too. Data classes, for instance, don’t need constructors, getters, builders, mutators, or any of the verbose code expressions you might see in a Java data class. Instead, most Kotlin classes can be declared in a single line. This has the effect of making the code simultaneously shorter and easier to understand.

Kotlin also has measures in place to catch and eliminate null pointers. In Kotlin, nulls are illegal for standard data types. Instead, you’ll need to use a ? to denote a separate nullable type if null values need to be allowed. This makes Kotlin code more secure and eliminates the pesky NullPointerException in Java.

 

Kotlin & Java Interoperability

Just because Kotlin looks and feels slightly different than Java when you’re reading and writing it doesn’t mean that it’s functionally different. In fact, Kotlin is 100% compatible with existing Java code. It compiles to the exact same bytecode, making it interoperable in the JVM or on Android. Kotlin programs have complete access to all existing Java frameworks and libraries. That even applies to frameworks that rely on annotation processing or other advanced prerequisites.

There are no wrappers or adapter layers involved in using Kotlin and Java side by side. In fact, developer tools can automatically convert your Java code to Kotlin code without the need for you to do any rewriting by hand.

 

Functional Language & Asynchrony

Since Java and Kotlin code can live together seamlessly in the same project, it’s possible to have both and work in both. However, Kotlin does have some functional advantages.

One example is Kotlin’s relaxed stance toward functions being class members. With Kotlin, you can declare functions at the top level of a file, locally within another function, inside a class or object as a member function, or as an extension function. This means you can use functions to extend a class without having to inherit from that class.

Another Kotlin feature is the ability to use asynchronous mechanisms available in other languages. Kotlin supports coroutines out of the box that allow for asynchronous programming by pushing the complications into libraries. The coroutines express the logic of the program sequentially while the underlying library sorts out the asynchrony for us. As a result, coroutines can help Kotlin code implement C#’s async/await, channels and select from Go, or Python’s generators/yield.

 

Android Support & Community Growth

With official support from Google, Kotlin is poised to take over Android development. The situation is similar to Swift’s dethroning of Objective-C in iOS app development. Swift was so much simpler in syntax while still having strong features. It made sense to use Swift. The same applies to Kotlin and Java for Android development.

New apps could be fully developed in Kotlin from the ground up. However, since Kotlin has 100% interoperability, it’s not necessary to rewrite existing apps from scratch. Instead, Kotlin can incrementally become a part of an Android app’s code. Thus, the transition will be gradual as more code gets written in Kotlin over time and Java slowly fades out of use for Android apps.

Kotlin code is safer and cleaner than Java code. Additionally, it only takes a Java developer a few hours to learn and start using Kotlin. The benefits are enormous. Get rid of null pointer errors, make extension functions possible, and enable coroutines all in one.

 

Kotlin Is Poised for Growth

The key takeaway here is that Kotlin can complement, replace, or extend much of Java. It’s flexible and available for any context where you would typically use Java. Additionally, adopting Kotlin is low risk. It’s a small time investment for devs to learn Kotlin, and the payoffs could be enormous in reducing errors and enabling functionality. What’s more, you can try Kotlin in a small part of your existing Java code base with no repercussions for the rest of the project, since Kotlin exports a Java API where the bytecode is indistinguishable from Java code. With its simpler syntax and added features, expect Kotlin to increasingly play a role in projects that use Java.

Follow Us

Categories

Intertech Consulting
Course Finder
Design and Code Review Checklist

Also published on Medium.

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!

Pin It on Pinterest

Share This