DEV Community

loading...
Cover image for Kotlin Vs. Java

Kotlin Vs. Java

jason_kane7 profile image Kane Jason ・4 min read

Java, with 20 years of successful history, has a higher number of Java-based Apps in the Play Store. With its own set of advantages, the programming language Kotiln, introduced by JetBrains, has succeeded to mark its presence dominantly. Both of these powerful languages are considered the official languages for Android App Development by Google.

Java has been the first choice of many companies for years, definitely for the obvious reasons like platform independence, robustness, cross-platform capabilities, etc, Kotlin, being a java-based language with even higher productivity, is also gaining so much popularity. Let’s get out the disarray and look at the behavior of Java and Kotlin over the criteria crucial for an Application performance.

Interoperability

If you are already working on a programming language and are acquainted with all the technical nitty-gritty, switching to any other language might haunt you and hold you back from advancement. The Interoperability of the language proves to be extremely helpful in this case.

Being compiled to bytecode and compatible with JVM just like Java, Kotlin is highly interoperable with Java and empowers you to use both the languages in unison that is using the Java code within Kotlin or vice-versa. To start with Kotlin, you need not start your project from scratch, rather you can use the modules in both the languages i.e. Kotlin or Java simultaneously, merely by installing the Kotlin plugin. Not only this, but you can also easily convert your Java program to Kotlin through the J2K converter included in the plugin. On the flip side, if we compare Java vs Kotlin, Java does not offer any such facility, requiring you to start from scratch itself, hence lags behind Kotlin in this context.

Code Brevity

Code Brevity, one of the very important factors considered in the structure of the program, making the code more comprehensible, cleaner, and is less prone to bugs thereby improving the quality of the code.

Alt Text

Kotlin requires comparatively less number of LOC as compared to Java, making it more readable and understandable. For example, the ‘Switch case’ in Java has been converted to a fewer line code using ‘when’ in Kotlin.

Easier Thread Managements through Coroutines

Android, being single-threaded, creates the main thread when the app is started, on which all the components run. The problem generally faced in the asynchronous approach is that it blocks the thread until the requested task is completed, freezing the UI of the app for that time.

Coroutines, an experimental feature introduced in Kotlin, has addressed this problem by replacing the thread blocking mechanism with the thread suspension which can be resumed later, making it extremely useful for the long-run task. Unlike Java, it does not create stacks of thread in order to execute the time taking tasks, rather it is stackless and works on a single thread, making it lightweight, easy to manage, and more comprehensive.

Data Classes to reduce coding time

When you write a Java program, you create classes to hold data, constructors for object initialization, getter, and setter functions to update and retrieve values for various declared fields, etc. which is a huge amount of code, you write for every program.

Alt Text

Kotlin liberates you from this, through Data class, which provides you a lot of self-generated data such as equals()/hashCode(), getter, setters, etc, enabling you to do the same task with a fewer Line Of Code. Data class not only reduces the boilerplate volume and the complexity but also saves a lot of time for the programmer. In Kotlin, if you want to create a code for a person’s detail, you can simply do this in one line of code, rather than declaring the variables separately then applying getters and setters, and a lot more.

No Implicit Widening Conversions

Implicit Widening is the process of converting the data type implicitly, For Example: if an operand in the addition operation is of type Double, the other operand is automatically converted to Double type.

Java supports the Implicit Widening Conversion, however, you need to do the conversion explicitly in case of Kotlin as it does not support the implicit conversions in order to prevent the loss of precision. A function in a Kotlin program, with a Float parameter, cannot be called on Int value, but only on Float value.

Null Safety

Null References, considered as a billion-dollar mistake by its creator Tony Hoare, is the most general problem faced by the Java Programmer. Null References generated NullPointerException, requiring you to invest your valuable time in spotting it in the huge codebase. Kotlin relieves you from this pain, providing you with the null type in the code which you can easily declare when required.

Popularity

Java has been one of the most preferred languages for quite a few years, in fact, many developers are still using it for their projects. The popularity of Java has seen a bit of downfall after the introduction of a similar yet highly advanced language that is Kotlin. Kotlin, due to its incredible functionalities and ease of use, has caught a lot of attention, making it the choice of around 72.6 % of developers, according to Stackoverflow. On the contrary, Java managed to gain only 53.4% of votes, clearly indicating the popularity of Kotlin.

Conclusion

The Boilerplate volume, Execution time, Code Conciseness, Ease of use, Understandability, Effectiveness, Error Handling, etc, are some major factors that contribute to the better performance of an application. Though Java has been the choice of many app developers since it was introduced, however, its complexities and drawbacks cannot be denied either.

Kotlin, by removing all these complexities and introducing the improved as well as some advanced features, comes out a winner here. Kotlin is the future of Application Development and the best part about Kotlin is that it does not compel you for the immediate complete switch, rather allows you to head gradually.

Source: https://www.aceinfoway.com/blog/kotlin-vs-java

Discussion (1)

pic
Editor guide
Collapse
khmarbaise profile image
Karl Heinz Marbaise

Based on your post I can see some parts are not really accurate. First Java has 25 years of history.

Apart from that I've seen the example of switch/case vs. expressions in Kotlin. In JDK 13 this is available in Java as well.

Based on your point about data classes which are available with JDK 14 (called records) which exactly do that.

You have created a Java with everything implemented getters/setters .. You usually decide what you like to have either an immutable data class (having only setters; yes you have to write hashCode/equals but that can be handled via Lombok (just an annotation) or just a few keystrokes in your IDE).. (or with records).

About the threads I see things on the way like fibers in Java which are great improvements vs. usual threads. Coming in JDK16 on march 2021..so not far away. Apart from that it looks like you are referencing here Android and not JVM in general? Maybe I misunderstand a thing here.

The interesting part about: "Null Safety"

Null References generated NullPointerException, requiring you to invest your valuable time in spotting it in the huge codebase.

Yes of course, cause you hadn't have valuable tests to spot such things before ...(using unit tests; mutation testing etc.) also helpful code reviews / pair programming that prevents many of such things.

Kotlin relieves you from this pain, providing you with the null type in the code which you can easily declare when required.

I can say that good designed code using tests etc. the null type or the absent of null would not change a thing. The problem is usually bad designed not really tested code etc. It's not based on the language.

You can always make mistake in any language.

What you have mentioned about a function in Kotlin:

A function in a Kotlin program, with a Float parameter, cannot be called on Int value, but only on Float value.

This sentence implies (based on the parts before) that it is possible to have that in Java which is not correct. That will fail in Java as well.

Some words about the "Popularity" in your article.
In your original article you have referenced the statistics on Stackoverflow by using the title "Kotlin Vs Java Popularity: Most Loved Programming Languages 2019"
but the original title on Stackoverflow: "Most Loved, Dreaded, and Wanted Languages"