Java vs. Kotlin: The raging debate
|Ashish Parmar in Android Thursday, December 13, 2018|
Deciding if you should use Java vs. Kotlin for Android development is a debate that is still fresh with pros and cons on either side. But despite the argument, Kotlin marches on with a strong developer force behind it, replacing regular Java as it strengthens.
Java and has been around for 22 years and was the go-to language for Android developers until recently. Experts state that it could become obsolete even with the latest Java 8, mostly because of its endless try-catch blocks, verbose syntax, lack of support for functional programming features, null-unsafety and the notorious NullPointerException. Also, Java was created to be a backward compatible language, meaning that the newly launched versions have to support the features of the previous ones. This layered architecture made it harder to add new features as this increased the amount of code substantially.
Kotlin’s biggest strength and one of the biggest advantages of Java users is its interchangeability with Java. A project can be compiled perfectly with both codes existing side by side. Learning Kotlin for an experienced Java developer will not take more than a couple of hours and the flexibility allows for incremental transitioning. Java developers would find Kotlin syntax familiar. With seamless interoperations with Java, Kotlin works well with Java libraries, especially RxJava. RxJava and Kotlin deliver an imperative programming environment with functional features. RxKotlin has some added extension functions to Kotlin and is built on top of RxJava. The reverse is true as well; any library written in Kotlin is easy to include within Java code.
There are some exceptional features of Kotlin for Android app development that make it stand out in a crowd.
Features of Kotlin for Android app development
- Null safe by default. Java developers face huge issues with NullPointerExceptions. The problem with Java is that it allows you to assign a null to any variable. Though if you try to use an object reference that has a null value, you will encounter a NullPointerException. In Kotlin, no variable is nullable by default. Trying to assign or return null in your Kotlin code as in Java, will make it fail at compile-time. To assign a null value to a variable, you need to explicitly mark the variable as nullable by adding a question mark after the type. This renders a NullPointerException almost impossible to encounter except when explicitly asked or when it originates from an external Java code.
- Extension functions. An extension function is a great feature that comes from C# but is missing in Java. Kotlin allows you to add behavior and new functionality to a class without actually directly extending it or employing a design pattern. Just prefixing the name of the class you want to extend to the name of the function you are creating will create extension functions. With an extension function, you can call a function from an object on instances of the extended class, with the “.” notation, as if it was a part of its class.
- Higher-order functions and lambda expressions. One of the biggest reasons why Kotlin is a great alternative to Java on Android is its main feature - higher-order functions. A higher-order function takes functions as parameters or returns a function. One case of major use for this is the callback functions. When you create a function to make a network call with two callbacks - one for success and one for error, in Java, one has to follow the pattern of having an interface and pass an instance to implement that interface, requiring a lot of coding. In Kotlin, it would be just two parameter functions. Functions are stored in variables for subsequent usage, passed around or created inside another function. When an undeclared function is passed immediately as an expression, it is called a lambda, an anonymous function or function literal. Though Java 8 added support for lambdas, one is stuck with Java 7 for Android app development.
- Immutability. In a multithread application, when a variable is mutable, it can be changed by any thread that accesses it. This implies that when there are multiple sources making changes to the data, manual synchronization needs to be implemented. This avoids data corruption but greatly increases code complexity and execution time. When the data is unchangeable, multiple threads can access it without error, as the data is immutable. Kotlin allows you to declare variables with keywords var and val. Var declares a re-assignable variable and val declares an unchangeable variable. This ensures the developer and the compiler that the variable cannot be reassigned.
Read: Why Kotlin language use is skyrocketing
Despite all these advantages, Kotlin’s biggest selling point is its succinct coding. It requires about 20% less coding than Java. This concise and compact coding entails fewer bugs. This makes boilerplate coding in Kotlin much more enjoyable as compared to Java. Kotlin Android extensions, in particular, allow the developer to import the reference to a View into an Activity file, to work with the View as if it were a part of that activity, instead of identifying each view with FindViewById.
Kotlin undeniably increases product quality and productivity. Many users are slowly transitioning to Kotlin programming with each update. With Google’s I/O keynote announcement to make Kotlin a first-class language for writing Android apps in May 2017, garnering the loudest applause, the debate about a Kotlin being a complete replacement or an add-on package for Java might be one that is not going to settle anytime soon. With Java still having the upper hand and the popularity built over two decades, it may be too early for its death knell.
This content is made possible by a guest author, or sponsor; it is not written by and does not necessarily reflect the views of App Developer Magazine's editorial staff.
MEMBERS GET ACCESS TO
- - Exclusive content from leaders in the industry
- - Q&A articles from industry leaders
- - Tips and tricks from the most successful developers weekly
- - Monthly issues, including all 90+ back-issues since 2012
- - Event discounts and early-bird signups
- - Gain insight from top achievers in the app store
- - Learn what tools to use, what SDK's to use, and more