My first time with Kotlin
The first time I tried Java was in summer 2012 when I had my first experience in Android Development. Since that time I have never used anything else for Android development. I had a short experience with PhoneGap when I wrote a cross-platform app for iOS and Android and it was a nightmare I’m still trying to forget.
So I can say I have never used any other language to create an Android application. I like Java and I think it’s a simple enough language. But it has lots of things I don’t like and I would like to change.
One of them is that despite the fact that everybody already has been using Java 8 for very long time, we still have to use Java 7. Even if the new Android Studio started to support Java 8 we still are not able to use all of the new features and there are still some troubles. That’s really annoying.
Another annoying thing is that if you want to write good, consistent code according to the code style, you have to make all fields private, create lots of getters and setters and all of them should have JavaDoc comments if they are public (Yes I write comments). This is because Java doesn’t have properties and getters and setters are not being generated automatically. You can ask Android Studio to generate them, and maybe even use some plugin to add some comments automatically. But all these unnecessary methods are still in your file and take too much space. Sometimes even more than the other code.
Besides these, there are lots of other little annoying things that Java developers don’t have and that could make their life easier.
The little things… there’s nothing bigger, is there? David, Vanilla Sky, 2001
I’m not going to make a review of Kotlin or make the full comparison with Java. I will just tell you a few things that will make you want to go deeper and deeper into the world of Kotlin and never come back or at least, question your unrequited love for Java.
So here are few examples of the differences you’ll experience immediately after trying Koltin.
Kotlin has properties and finally, you will have default setters and getters. Amen.
public class User {
public var name: String? = null
…
}var user: User = User()
user.name = “Jhon”
A feature you always dream about, only to wake up to disappointment. Or is it just me? That’s right. Kotlin has singleton from the box.
object DataProviderManager {
fun registerDataProvider(provider: DataProvider) {
// …
}
}
Simple. Right? Of course, it’s was never a big problem to write singleton. But I’m in love with this one.
Kotlin has Nullable types and Non-Null Types.
“Kotlin’s type system is aimed at eliminating the danger of null references from code”.
var a: String = “abc”
a = null // compilation errorvar b: String? = “abc”
b = null // ok
Now, if you call a method or access a property on a, it’s guaranteed not to cause an NPE, so you can safely say
val l = a.length // ok
But if you want to access the same property on b, that would not be safe, and the compiler reports an error:
val l = b.length // error: variable ‘b’ can be null
Kotlin has **Extension function**.
Extension function provides the ability to extend a class with a new functionality without having to inherit from the class.
fun String.isNumber() {
val tmp = this
var isNumber: Boolean = false
//check if this is number.
return isNumber
}
Now, this method can be called for any String object. Also, you can create an extended function for any class.
These are just the simplest examples of Kotlin’s advantages. But, of course, there are still some problems like the fact that it’s not compatible with all libraries. For example, to use a new annotation from greenDao you still need to use Java. But this is not a big problem, you can combine Java and Kotlin.
In my opinion, Kotlin is cleaner and more powerful than Java. I don’t know if Google has plans to make Kotlin the main language for Android development, but we definitely need some language like that.