I want to explain why I prefer Kotlin over Java.
In short, the many myriad Kotlin features enable more concise and understandable code than Java without sacrificing performance or safety.
Kotlin compiles to bytecode, so it can perform just as well as Java. It’s got the same compile-time checks as Java (and more, such as built-in nullability checks). And most importantly, Kotlin’s language features and standard library functions enable succinct, effective code.
I’m bullish on brevity because I think it’s a key factor in programmer productivity.
In the beginning, there was assembly. Each line of code gave you but a mere spec of what the entire program would do. That made it both difficult to read and write because you had to keep so much code in your head at once.
Higher-level languages allow us to put more ideas into each line of code. For example, sorting a list is trivial in most modern languages. It’s easier to write larger programs when you’re given more power per line of code.
We don’t want to pack as many ideas into each line of code as possible, though. If code is too terse, then it also becomes difficult to understand. Regex is a classic example of this problem; it is terse to the point that I still struggle with it regularly.
When comparing Kotlin to Java, I believe that Kotlin is more succinct than Java. But here’s what’s crucial: Kotlin doesn’t sacrifice comprehension in order to achieve brevity.
A Brief Example
In Java, a well-written, immutable value class involves implementing constructor(s), getters,
toString(). You might also want a builder and/or mutator to make it easier to work with.
I’m not going to bloat this article with a Java sample; you know how long that’d take. Let’s instead check out how easy it is to do all of that using data classes in Kotlin:
// ctor, getters, hashCode(), equals(), and toString() data class MonthYear(val month: Int, val year: Int) // Named variables replace builders val now = MonthYear(month = 5, year = 2017) // Mutation via copy() val nextMonth = now.copy(month = 6)
Data classes are clearly more concise than Java’s value classes (even if you’re using AutoValue). At the same time they’re easier to understand as well: from the single example above you’ve already learned most of what there is to know about data classes.
It’s this sort of win-win scenario in Kotlin – code that’s both more concise and easier to understand at the same time – that won me over. The language is full of these features and functions that enable me to write better code.
As an Android developer, there’s another important reason for my preferences: Kotlin can be updated independently from the OS.
New Java features (such as the streams API) help close the gap between Java and Kotlin, but since Java capabilities are tied to the Android OS, you often miss out on new language features and standard library APIs. By comparison, you can start using any new Kotlin features the moment that they’re released.
A good language is worth nothing if the tools and support aren’t there for it. As such, another important aspect of Kotlin is its good development environment.
First, it’s developed by JetBrains, who are known for making great developer tools. As such they’ve ensured that Kotlin has all the tools you need to do serious production development.
And now, Kotlin is officially supported by Google for Android. (I wrote this article before this announcement… good timing, eh?)
If you’re anything like me, you’d rather immediately jump into coding rather than read any more details about the language. Here’s where I picked up Kotlin:
- Kotlin Koans – a series of simple coding exercises that demonstrate basic Kotlin features. You can even do them online now.
- Advent of Code – A fun series of coding puzzles that are great for learning any new language. I did last year’s puzzles in Kotlin to solidify my skills.
- Start using Kotlin in your Java codebase. Kotlin and Java are interoperable, so it’s easy to do a trial of Kotlin on just a small part of your codebase.
thanks you RSS link