IndustryShould I Learn Kotlin or Java?

Ben Deitch
writes on July 31, 2017

With Android officially adopting Kotlin as a supported language, there’s one question that every new Android developer should be asking themselves: should I learn Kotlin or Java?

Rather than burying the answer all the way at the bottom… in my mind, it’s definitely Kotlin.

Kotlin was introduced in 2011 by JetBrains (maker of IntelliJ IDEA, PyCharm, and many other top IDEs) who at the time were using almost entirely Java. They created Kotlin because, “First and foremost, it’s about our own productivity”.

kotlin island*Kotlin is named after Kotlin Island in St. Petersburg, Russia

So Kotlin was created explicitly to be better than Java, but JetBrains wasn’t about to rewrite their IDEs from scratch in a new language. Which is why they made Kotlin 100% interoperable with Java. Kotlin runs on the JVM and compiles down to Java bytecode; you can start tinkering with Kotlin in an existing Java or Android project and everything will work just fine.

That said, there aren’t yet many Android tutorials using Kotlin; most beginner stuff is in Java along with the Android docs (though we’re working on our first beginner-focused Kotlin for Android course, and it should be out before too long). So to figure out how the Android system works and what it looks like behind the scenes, you’ll have to spend some time with Java.

Now let’s get back to that productivity part. There are plenty of articles out there showcasing what Kotlin can do, and there’s even a really good Java/Kotlin comparison on the Kotlin website (which has excellent documentation). So rather than creating an exhaustive list of what makes Kotlin so awesome, I’m going to show you my favorite example of Kotlin superiority which I borrow from our Kotlin for Java Developers course.

Creating a Card Class

Let’s say we need a class to represent the state of a playing card. It would need 3 properties: the value of the card, the suit of the card, and whether or not the card is face up. Also, since we usually deal cards face down the ‘faceUp’ property will typically be false, so we shouldn’t need to specify it every time.

Here’s what that looks like in Java. We need three fields as well as associated getters/setters, along with two constructors.

Here’s what it looks like in Kotlin:

class Card(val value: Int, val suit: String, var faceUp: Boolean = false)

33 lines down to 1! Not bad, right? But it gets better. In Java (and Kotlin), if you try to print out an object, instead of seeing the properties of the object you see the object reference:

And if you try to compare these two cards, you’ll end up comparing object references, and it’ll be false:

println(card1 == card2); // false
println(card1.equals(card2)); // false

In Java, if you want to test for equality between objects you need to override the ‘equals’ method:

And if you want to see something useful when you print the object, you need to override the ‘toString’ method:

But in Kotlin, all you have to do is add the word ‘data’ in front of your class:

data class Card(val value: Int, val suit: String, var faceUp: Boolean = false)

Here it is in action:

We’ve now got 51 lines of Java, and still just 1 line of Kotlin!

smilies

In summary, learn Kotlin. But if you’re completely new to programming, start with Java first. Most Android code is still written in Java, and at the least, understanding Java will be a boon for understanding the docs. Once you’ve got the basics of Java, you’ll be able to pick up Kotlin that much faster and will have a greater appreciation for the benefits that Kotlin brings. On the other hand, if you’re an experienced developer check out our Kotlin for Java Developers course. It teaches you everything you need to know about Kotlin by building a headless solitaire app!

Ps. If you’re looking for a list of reasons Kotlin is better than Java, Magnus Vinther does a great job of breaking it down in this medium post

7 Responses to “Should I Learn Kotlin or Java?”

  1. muhammad on August 2, 2017 at 4:32 pm said:

    so if it compules to java and then byte code so its slower than java right ?

  2. Muhamed Hassan on August 1, 2017 at 9:42 pm said:

    I was thinking about coding a card game app in java and then i saw this XD

  3. Erick Heredia on August 1, 2017 at 11:25 am said:

    Awesome! I want to develop Android apps with Kotlin. Is Treehouse planning to release a track for Android development using Kotlin?

    Regards,

    • Ben Deitch on August 2, 2017 at 9:29 am said:

      We’re not entirely sure what form it will take, but there is certainly more Kotlin/Android material on the way!

  4. Miguel Laranjeira on July 31, 2017 at 9:31 am said:

    With the data keyword how does it know which fields make an instance unique?
    If the one card was face up would you then need to override the Equals method?

    Just curious as I’m considering Kotlin currently, but haven’t had enough time for a deep enough look.

    • Ben Deitch on August 7, 2017 at 10:39 am said:

      It checks all the properties against each other. So if you wanted a face down card to equal a face up card, you’d need to specify that. Though in this case (a solitaire app), since we’re only using one deck, there’s not much need for the equals method.

Leave a Reply

Want to learn more about Android?

Android is the most common mobile platform in the world. Learn about the powerful development tools from Google, and how to create Android apps using Java.

Learn more