Java vs. Kotlin: The raging debate

Posted on Thursday, December 13, 2018 by ASHISH PARMAR

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.

Just as Java is named after an island in Indonesia, Kotlin gets its name from an island near St. Petersburg, Russia. It has been 6 years since the arrival of Kotlin, but it has only been commercially available for a little over a year. Developed by JetBrains programmers, Kotlin is a statically-typed programming language that runs on JVM (Java Virtual Machine) and can also be compiled to JavaScript source code or uses the LLVM compiler infrastructure. Its biggest advantages are its interoperability with Java, concise codes, excellent Android Studio support, easy learning curve, extension functions, lambda expressions, data classes, co-routines, immutability and type aliases.

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.

More App Developer News

Sidewinder telescope accessory mounting rings from Rouz Astro



Draco 62 quintuplet imaging refractor telescope from Founder Optics



PS Align Pro and Xasteria Weather app updates



Social engineering takeover attacks are on the rise



Epic Games defeats Google in court



Copyright © 2024 by Moonbeam Development

Address:
3003 East Chestnut Expy
STE# 575
Springfield, Mo 65802

Phone: 1-844-277-3386

Fax:417-429-2935

E-Mail: contact@appdevelopermagazine.com