Kotlin vs Java

In this post, I compare Kotlin and Java and explain why in my opinion Kotlin is the way forward.

Feature Java Kotlin
Checked exceptions Yes No
Primitive types Yes No
Static members Yes No
Package-private access level modifiers Yes No
Big community Yes No
Resources (Books, posts..) Yes No
Functions with default and named parameters No Yes
Extension functions No Yes
Operator overloading No Yes
Primary constructors No Yes
Singletons No Yes
Range expressions No Yes
Null-safety No Yes
Smart casts No Yes
String-handling functions No Yes
Data classes No Yes
Destructuring Declarations No Yes
Statically typed language Yes Yes

Things that are not in Kotlin that I don’t miss from Java

Since I started using Kotlin, I’ve found that most of the time, you can use Kotlin to do anything that you used to do with Java, but in a slightly different way. However, Java is getting old so some of its features are only there because it is backward compatible. In practice, what that means on the positive side is that you can still run a program done in Java 1 with Java 8, but the negative is that you still needs to include dated things like “;” at the end of each statement.

Here a few examples of features that you cannot find in Kotlin but which I personally don’t miss anyway:

  • Checked Exceptions: In my opinion checked exceptions are not useful, they make the code more verbose and less readable. This results in multiple throws clause declarations, and more generally they aren’t compatible with functional coding.
  • Primitive types: Kotlin has primitives too (similar to Java) but unlike Java, they are not a special case. In Kotlin, everything is an object, including basic types.
  • Static members: Kotlin has replaced these with package-level (also known as top-level) functions and companion objects. The official Kotlin website recommends using package-level functions in most instances. These are functions declared outside of any class, so there is no need for an extra level of nesting.

Features that are available in Kotlin and should have been available in Java

  • Functions with default and named parameters: In Kotlin you can define a function with default parameters. This way you can call a function and only have to specify the parameters that are different to the default ones:
  • [java]
    fun defaultSum(p1: Int, p2: Int = 10, p3: Int = 0): Int {
    return p1 + p2 + p3

    assert(3 == defaultSum(1, 2))
    assert(13 == defaultSum(3))
    assert(15 == defaultSum(3, p3 = 2))


  • Extension functions: these are functions that can be called as a member of a class but are defined outside of it. This feature makes it easier to integrate with existing code. For instance, in Kotlin you can add a new
    function to the Java String class and use it in your code.
  • Operator overloading: similar to other languages, Kotlin allows you to overload operators. For instance, you can define a special method plus to overload the + operator.
  • Null-safety: in Kotlin, the compiler will be in charge of checking any possible NullPointerException.
  • Smart casts: when you check if an object conforms to a given type, then the compiler already knows the type of the object. Therefore you don’t need to cast it. Let’s have a look to an example:
  • [java]
    if (x !is String){
    print(x.length) //there is no need to cast it

  • Singletons: it is possible to build singletons with Java, but Kotlin provides first class support for creating singletons. You just need to use the word “object”.
  • Range expressions: Kotlin makes it easier to define a range by using the .. operator (ex: for (i in 1..4) ). Other keywords such as downTo (for reversing the order), step and until could also be very useful.
  • Data classes: If you mark a class as “data”, then the compiler automatically generates the following methods from all properties declared in the primary constructor: toString, equals, hashCode and copy.
  • String-handling functions: Kotlin provides an extra number of functions and templates that makes it easier to work with Strings.
  • Destructuring Declarations: this feature allows you to unpack a single composite value and use it to initialize several separate variables. For instance, you can ‘destructure’ an object and assign it to 2 variables: val (x,y) = Point(10, 20)

In summary

In my opinion, although Kotlin doesn’t have one single feature which will single-handedly blow your mind, when put altogether, the collection of new features that Kotlin offers really make a difference. Kotlin removes a lot of the boilerplate code that you have in Java and allows you to spend more time on the interesting parts of the code and less on routine matters.

Kotlin is not just object-oriented but also functional too. A functional style provides extra conciseness, safer multi-threading and is easier to test. Most of the most popular features of functional languages are available in Kotlin, such as first-class functions and immutability. Although since Java 8 came out, Java also has had the functional style available, Kotlin provides syntactic and library support, meaning that you can use the functional style effortlessly.

The fact that Kotlin is fully compatible with Java is also one of its biggest assets. Existing Java code can be called from Kotlin in a natural way, and Kotlin code can be used in Java too. This gives you the flexibility of mixing Java and Kotlin anywhere in your project. However, I personally wouldn’t recommend mixing them, at least not in production code, because it can overcomplicate things. Nevertheless, you could introduce it for testing purposes. Currently, there are very good frameworks which offer a more expressive DSL for writing tests in Kotlin like KotlinTest and Spek.

One field in which Java is definitely much better than Kotlin is in the quantity of available helpful resources. Java is more than 20 years old and therefore has an innumerable amount of books, articles, posts, etc written on the topic. Kotlin it is growing very fast, but still has a long way to go before it reaches the same size of community and resources as Java.

In conclusion, Kotlin is a great language with lots of fantastic features that make coding much more pleasant and improves the quality and readability of the code. Having said that, my advice is that if you are already working on a Java project, I wouldn’t attempt to mix both languages within it. On the other hand, if you are planning to start a new project or a new microservice then I would definitely recommend using Kotlin from the very start.

You may also like...

1 Response

  1. Niels S says:

    I just read this article and thought to myself: “How could he forget about properties?”

    Seriously – I think this is the most valuable feature (next to null-safety) of the Kotlin language.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.