With Android officially adopting Kotlin as a supported language, there’s one question that every new Android developer should be asking themselves: should I be learning 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.
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!
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.
The post Should I Learn Kotlin or Java? appeared first on Treehouse Blog.
from Treehouse Blog http://ift.tt/2tQWbkq
via https://ifttt.com/ IFTTT