DEV Community


Why I prefer Kotlin

Graham Cox
・2 min read

I've been an enterprise Java developer for a little over 10 years, and I've been using Java for just over 16 years - back when Java 1.3 was the latest and greatest. It's been a long ride. Back then there were no Lambdas, no Streams, no Generics, no Annotations. There wasn't even an assert keyword.

These days, Java has a huge amount for a very vast and powerful ecosystem. And one of the most powerful parts of all of that is the built in support for alternative languages as part of the same system. The JVM makes it possible to have many different languages - meaning the actual code you type into the editor - compile down to compatible bytecode and all run together in the same application. That's a huge benefit for software development, since you can now write different parts of the application in the best suited language for the job.

So what exactly is Kotlin, and why do I like it so much? Unlike many of of the other JVM languages, Kotlin doesn't try to be anything special or different. It simply exists as a vastly simplified, streamlined Java language. There is very little, if anything, that Java can do that Kotlin can't do. The difference is that Kotlin does the same in significantly less code, making it significantly easier to read and maintain. Kotlin also has a large emphasis on Java Interop, meaning that it's trivial to use it in conjunction with existing Java libraries. Some other JVM languages make this much harder to achieve, if it's possible at all.

So, in general, my experience so far is that Kotlin allows me to do everything that Java does, but simpler.

The only thing that I've struggled with at all was some of the tooling - but this really doesn't bother me that much. By this, I'm talking Code Coverage (The JVM bytecode includes many lines that just don't exist in the Kotlin code, so covering them is less easy), FindBugs, PMD, Checkstyle, and so on. I've not explored Sonar for Kotlin yet, but it's very likely that this will fill in many of the gaps if it works.

Discussion (10)

jgoodman751 profile image

Enjoyed your article. Could you share some code examples were kotlin has helped you?

grahamcox82 profile image
Graham Cox Author

Data Classes are the biggest, most obvious candidate. You can write a Java Bean - with Fields, Getters, Setters, Constructor, Equals, Hashcode, ToString and some extra convenience methods - in a single line of code.

Other than that, pattern matching is a huge win for me, and blocks as expressions. Type Inference is useful but it's not the killer feature that some people make it out to be - IntelliJ does most of that typing for me anyway.

I might end up spending some articles in my "100 Days of Writing about Code" covering these in more depth when I run out of things to say :)

stealthmusic profile image
Jan Wedel

Have you tried Lombok with Java? Since we started using it, my team loves it. @Data is what you want.
The only thing I'm missing from Erlang is Pattern matching, but at method signature level. I think also Kotlin can't do it, right?

Thread Thread
grahamcox82 profile image
Graham Cox Author

I used to use and like Lombok, but less so now. It looks and reads like real Java but does things behind the scenes, which isn't obvious if you don't know about it. Using a different language is much more obvious what's happening, even if it's much bigger.

Data classes are also only scratching the surface of the extra features - better lambdas, destructing, coroutines, etc. Even just template strings can be a huge boost if you're writing things like DAOs with complex queries in then.

askanison4 profile image

Seconded. I've not been a huge fan of Java but I'd never even heard of Kotlin. Would love to see some examples from someone evangelising it :)

thefern profile image
Fernando B 🚀

I've been meaning to learn Kotlin might integrate some classes with my current Java program. Do you have experience with groovy?

grahamcox82 profile image
Graham Cox Author

A little, but not enough to really know what I'm talking about yet :(

roger_b_m profile image

Thanks for the article--I wasn't aware of Kotlin, and have avoided Java for many of the reasons you like Kotlin for, so now there's something for me to try :)

rmorschel profile image
Robert Morschel

How does Kotlin compare to something like Groovy? Seems to be the same kind of thing?

grahamcox82 profile image
Graham Cox Author

Better in some ways, worse in others.

My understanding of Groovy is that it's closer to a scripting language, which can make it very good for certain tasks. However, from experience it can be more difficult to get Interop between Groovy and Java code - especially in the direction of Java code calling Groovy code. Kotlin emphasis on Interop is quite a big deal, and they do a lot to make calling either direction as painless as possible.

Forem Open with the Forem app