What Kotlin Fixes in Java
If you're not sold on Kotlin, this compilation of its most useful features, not to mention its interoperability with Java, might help change your mind.
Join the DZone community and get the full member experience.Join For Free
It is a general purpose open source programming language for the JVM as well as Android and combines the features of both functional and object-oriented programming. One of the main reasons behind Kotlin’s popularity was its perfect compatibility with Java 6. That helped Kotlin make a name for itself as a suitable alternative to Java.
Then, last year, Google announced Kotlin as a first-class programming language for writing Android apps. After that, Kotlin's usage doubled within few months. Android Studio 3.0's support included creating new Kotlin files and the conversion of Java code to Kotlin. Google is putting weight on Kotlin’s foundation, as this programming language can support various features that currently are not supported by Java.
Java Issues Addressed by Kotlin
- NullSafety – The Billion Dollar Mistake is the name given to the danger of null references in code. Kotlin’s type system is aimed at eliminating the danger of these null references. This has been one of the most common pitfalls in Java — and many other programming languages as well.
- No more Raw Types – Kotlin is designed with Java interoperability in mind. So now, existing Java code can be called from Kotlin in an effective way. This allows the calling of Java code from Kotlin. Alternatively, Kotlin code can be used in Java rather smoothly.
- Invariant Arrays – The basic types used in Kotlin are Numbers, Arrays, Characters, and Strings. Unlike Java, the arrays in this programming language are invariant, meaning that Kotlin does not let a user assign an Array<String> to an Array<Any>. This prevents a possible Runtime Failure, which is one of the issues faced in Java.
- FunctionTypes – In Kotlin, a lambda expression or an anonymous function can access the variables declared in the outer scope. That is opposed to Java’s SAM-conversions — Kotlin has proper function types.
- Use-siteVariance – Wildcard Types are one of the trickiest parts of Java’s Type System. This issue does not occur in Kotlin — as it does not have any Wildcard Types, just Type Projections and declaration-site variances.
- Exceptions – Kotlin does not have any checked exceptions, as all exception classes in this language are the descendants of the class Throwable. And every exception has a message, stack trace, and an optional cause.
Why Choose Kotlin
Working with the mixed types requires knowing the type of an object at the Runtime in order to safely cast the object to the desired type — and, further, to call methods or access properties on it. For class casting in Java, we first check the type of the variable using the ‘instanceof’ operator and then cast it to the target type.
Whereas in Kotlin, when we perform an ‘!is’ or ‘is’ check on a variable, the compiler tracks this information and will automatically cast the variable to the target type where is the ‘!is’ or ‘is’ check is true in the scope.
Once in a while, a user needs to create an object of a slight modification of some class but without explicitly declaring a new subclass for it. Java handles this case with anonymous inner classes, but Kotlin generalizes the same concept by using object expressions and declarations. Just like the anonymous inner classes in Java, the code in object expressions can access variables from the enclosing scope. But in Kotlin, this is not restricted to final variables like in Java.
The whole purpose of creating classes is to hold data and in some classes — standard functionality with utility functions can be mechanically derived from that data. This is known as a Data Class in Kotlin. These classes generally contain some old boilerplate code in the form of toString(), hashcode(), equals(), setters, and getters.
Basically, Kotlin’s Data Classes are like regular classes but with some additional functionality.
Despite all of Kotlin's benefits, if you are asking the question, "Which is better, Kotlin or Java?" the answer is not simple.
Kotlin has come a long way, even if it is still in early stage of version 1.2. Kotlin code is safer and is more concise than Java code. Kotlin is not just useful for new apps, but its files can coexist with Java files — that means it also has scope for existing applications.
Of course, Java has been around for years, and the IT industry has an abundance of Java developers — but that’s not the case with Kotlin. The market is still struggling to cope with the demand of the language, and is not able to completely fulfill the supply portion.
But the fact that Kotlin is just getting started can’t be ignored for sure!
Opinions expressed by DZone contributors are their own.