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 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.
Supplemental Reading: An Absolute Beginner’s Guide to Kotlin
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.
See Also: SharedPreferences in Android with Kotlin
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!
Related: Java vs. Python: Complete Guide
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.
Want to further develop your coding skills? You’re in the right place. Try out the Treehouse 7-day free trial today!
Every one has different opinion. According to me, if one already knows Java then obviously it is better to learn Kotlin because one should always learn new technologies but if one doesn’t knows Java then he should start with Java as Java is the most basic language and it is not easy for any other language to replace it. Oracle has decided to release it’s updates in every 6 months which implies that the organization is trying hard to catch up the new updates.
so if it compules to java and then byte code so its slower than java right ?
Since they both use Java byte code, a Kotlin program will run just as fast as a Java program. Though it does seem to take marginally longer to compile a Kotlin project: https://medium.com/keepsafe-engineering/kotlin-vs-java-compilation-speed-e6c174b39b5d
I was thinking about coding a card game app in java and then i saw this XD
Awesome! I want to develop Android apps with Kotlin. Is Treehouse planning to release a track for Android development using Kotlin?
We’re not entirely sure what form it will take, but there is certainly more Kotlin/Android material on the way!
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.
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.