5 Reasons Java Users Should Consider Kotlin

Although it’s been six years since Kotlin first appeared, the programming language recently gained momentum when Google announced it would receive first-class support on Android. The latest iteration of Google’s home-grown IDE for Android development, Android Studio 3.0, fully integrates Kotlin; there’s even a conversion feature for developers who want to migrate existing Java files to Kotlin.

Kotlin is an open-source JVM (Java Virtual Machine) compatible programming language. For those frustrated with Java, it’s meant to offer some relief. “We have a huge Java codebase developed over a decade, IntelliJ IDEA, and we wanted to switch to a better language without abandoning the code we already have,” is how Kotlin project lead Andrey Breslav described the motivation behind Kotlin’s creation. “After examining existing alternatives, we found that none of them fulfills our requirements (that are rather moderate: smooth integration with the existing infrastructure, efficient tooling possible, good compiler and run-time performance).”

In short, Kotlin fans might describe the language as not only compatible with Java, but also much better. (And this despite Java’s massive footprint, which extends from Android apps and higher-end websites to electronic trading systems.)

Will Kotlin’s growing popularity doom Java? The latter sits in first place on the TIOBE Index’s ranking of the most popular programming languages, whereas Kotlin doesn’t even break into the top 20. So if Kotlin is going to eclipse Java, it’s going to take quite a long time. Nonetheless, here are some reasons why developers who use Java frequently should consider switching over.

It Can Replace Java in a Workflow

Kotlin fixes several issues with Java, including proper function types (in place of the SAM-conversions found in Java), use-site variance without wildcards, and no checked exceptions. Kotlin also offers numerous things not present in Java, including range expressions, string templates, null-safety, and smart casts. (To be fair, Java also offers some things that Kotlin does not, including static members and primitive types that are not classes.)

In other words, a developer sick of Java can transition (relatively) seamlessly to a Kotlin workflow. If the latter improves rapidly, the advantages may only grow. Java is notorious for relatively slow updating—it took nearly five years to go from Java SE 6 to Java SE 7, followed by two three-year gaps from 7 to 8 and 8 to 9—and it could find itself outpaced if the Kotlin community decides to iterate as early and often as possible.

It Fixes Some of Java’s Bigger Defects

Java is famous for its null pointer exceptions (once described as a “billion-dollar mistake” by Sir Tony Hoare, a British computer scientist). Kotlin and C# have very similar nullable and non-nullable references and do safe calls such as b?.data. Previously you would have accessed b.data, and if b was null, it blew up with an NPE. The b?.data only accesses data when b is not null, so no NPE.

One problem that Kotlin addresses is type safety in collections, typically lists. Before generics came along, it was all too easy to add a wrongly typed variable to a list. The compiler didn’t pick up on this, and it would blow up with an exception at run-time. Although generics were added in Java SE 5, this did not fully solve this problem. Java’s type system was unsafe.

It Takes Java Code in a New Direction

Java has traditionally been used to create desktop, server, web and Android applications. Kotlin takes this further by letting you compile to JavaScript and with Kotlin/Native to binary. This binary can be generated for iOS but doesn’t currently link to iOS libraries; but once that’s ready, you will have the ability to develop for both Android and iOS via Kotlin, which is a big deal. Currently you can only do so with C# (Xamarin), Scade and JavaScript.

Google Supports It on Android

Google and Oracle aren’t the best of friends, to put it mildly; Google used Java APIs in Android, which Oracle believes is a case of infringed copyright. The legal battle over that continues.

Given how many Android apps have been created since 2009 (3.25 million by September 2017), it’s a fair bet that Android development alone has kept Java near the top of worldwide programming language popularity. Android developers are generally using Java SE 6/7 to build apps, and they’re missing out on the newer features present in Java 8/9, including Lambdas, higher order functions, and Java Time APIs. But Kotlin offers those very features, meaning it can be used to build features for phones across the entire Android spectrum.

Kotlin’s appeal as a platform only increases when you realize that upgrading Android phones’ operating system version is difficult or even impossible. Google is taking steps to fix this in Android Oreo with Project Treble, but given how many Android phones there are out there, it’s questionable whether the effort will ultimately succeed. In the meantime, phones continue to run outdated versions of Android, including Marshmallow, Lollipop, and even four-year-old Kitkat.

It’s Fun to Program

Kotlin programs tend to be a lot shorter than their Java equivalents. There’s much less boilerplate code, particularly in data classes, which streamlines the Kotlin programming process in comparison to Java. Part of this stems from the Kotlin IDE: it’s slick, and its creators had many years to get that right (Android Studio is based on JetBrains Intellij Idea IDE).

Like Swift, Kotlin the Pascal order of variable declaration; with optional types, this makes it easier for the compiler to use inference when there’s an initial value in the declaration.

val varname : vartype = initialvalue

Semi-colons are unnecessary, making Kotlin programs easier to understand. Add to that smart casts, Properties (not just Getters and Setters), string templates and more. Once you’ve gotten used to these, going back to Java becomes a lot less fun.

Conclusion

The creator of Java, James Gosling, left Oracle seven and a half years ago over dissatisfaction with the company. Java’s direction is shaped by the JCP (Java Community Process); it works fairly well, albeit slowly.

Compared to Kotlin, Java now looks old and shabby. Well-known players such as Pinterest, Coursera, Atlassian, Evernote and Basecamp have all used Kotlin in their Android apps; as developers learn about the newer langauge and adopt it, it may well kick off Java’s decline, although Java remains near the top (if not at the very top) of various programming-language rankings. Google adding Kotlin as a “main” language for Android will only boost its popularity.

Post a Comment

Your email address will not be published.