Does Java really lag behind Kotlin?

Java isn’t as far behind Kotlin as you might think from reading those Kotlin vs java articles.

You may have seen thousands of articles praising Kotlin for having features that Java does not have. However, they are comparing Kotlin to Java 8 (which was released in 2014, and the latest release is Java 19). Indeed, some of the most promising Kotlin features are now available in the most recent Java version.

Here are some of the best Kotlin features that are also available in Java.

Data classes
In Kotlin, data class is a simple class which is used to hold data/state and contains standard functionality.
Java counterpart: Records (Java 14)

Sealed classes
In Kotlin, sealed classes and interfaces represent restricted class hierarchies that provide more control over inheritance.
Java counterpart: Sealed Classes (Java 17)

Coroutines
In Kotlin, coroutines are light-weight threads that allow you to write asynchronous non-blocking code.
Java counterpart: Virtual threads (Java 19)

Lambda expression
In Kotlin, lambda expressions and anonymous functions are function literals. Function literals are functions that are not declared but are passed immediately as an expression.
Java counterpart: Lambda expression (Java 8)

Auto closing resources
Kotlin has an extension function called `use` in its standard library. With it, once the reference object is no longer required, Kotlin will automatically close it.
Java counterpart: Try-with-resources (Java 9)

Private interface methods
An interface in Kotlin can have private functions and properties
Java counterpart: Private Interface Methods (Java 9)

Automatic type inference
In Kotlin, you can use `var/val` to declare a variable and compiler can automatically infer this type
Java counterpart: Implicit Typing with `var` (Java 10)

Multiline string literals
In Kotlin, you can define multiline strings in triple quotes.
Java counterpart: Text Blocks (Java 15)

I’m a big fan of Kotlin and will always prefer it over Java, but the comparison should be fair.

Share with your friends

Leave a Reply

Your email address will not be published. Required fields are marked *