Developing Android apps is a terrific way to grow your business, but deciding which programming language to use is the actual challenge. For many of us, Java is the finest because it is used everywhere and everyone appreciates it.
However, with the introduction of Kotlin, this apparent faith in Java has been shaken.
So before we get any further to the comparison between kotlin and java, here is some context. We are close (but distant) friends. It is my Birthday.
You posted me the hand-made Gift enclosed in a sturdy box. I received it, with a wide smile :) opened the box and I found....... nothing?!
My smile inverted and with a broken heart, threw the empty box away :( Still, wondering what the bananas just happened? Well, that's Java.
So what to choose Kotlin or Java?
Cracking it open
"You" - referred to you, the Android developer (who uses Java). That hand-made gift was your code (in Java). I am the JVM. And the box that I threw?
That was an exception - not an ordinary exception but a NullPointerException, the Death Eater, the Doomsday, the Goddess of Death, or whatever scares you! It just causes nothing to be in the place where I (JVM) expected something. [caption id="51770" align="aligncenter" width="445"]
JVM be like[/caption] I have been developing Android Apps for quite a while now but every so often, it happens at certain stages of my app's execution, it suddenly crashes. Reason? An object that I passed or received is null. Even worse, when the source of such crashes is because of a null object executed in the code of the 3rd party library!
When we try to access certain methods or values of an object when it's not even instantiated yet, it gives NullPointerException or NPE (yeah, it's that famous to have an official acronym). Just like the empty box that you sent me but don't feel sorry about that.
Here is a little bomb (as return gift) in Java. You would think that since you use an IDE, and your IDE is smart enough to point it out for you, you are safe. Well I hate to rain on the parade, but NPE is actually an unchecked exception, it extends RuntimeException, which means that you cannot guarantee that your IDE will be the Knight in armor for you and prevent any mishaps at compile time. It just cannot be done!
- Java has left the room. Sad.
- Kotlin entered the room. What?
Another fancy-named language with a more colorful logo, only to get stuck in the fan again? Nope. You see, Kotlin developers knew about this big shortcoming of Java and purposefully built it to eliminate this developing android app from ground-up. This is why even the basic syntax is built to prevent this. By default, every variable you declare needs to be initialized at that very moment with a value
var link = "https://terasoltechnologies.com" /* this is valid */Or it will not be able to get compiled. Similarly, if you want a variable to be declared now and initialize later, you will have to explicitly state that.
lateinit var link: String /* explicit declaration to initialize afterwards */A pattern that you might have noticed is that the former is more strict when it comes to handling null variables compared to Java. It puts more responsibility on the developer's side to make sure that the variable is not null when accessed. This alongside many other properties makes Kotlin more favorable than Java.
Is Kotlin == Java ?
But what if you have a huge code base written in Java, changing that to Kotlin would be hectic. Fret not, this is where the IDE comes in.
If you use Android Studio, you get an option to convert a Java file to equivalent kotlin statements. On Windows, open a .java file in Android Studio, then select Code > Convert Java File to kotlin File, and glittery kotlin code is right in front of you!
No! Kotlin > Java
It does not require a semicolon(;) after every statement, unlike Java.
This not only means faster typing but also a very little but significant decrease in size. And one of my favorite things about that is its interoperability with Java, which means.. (pause for the effect)... You can call functions from a Java file directly from a Kotlin file and vice-versa!
Crazy, right?! This is just the beginning.
I'll be writing a few more articles explaining even more such amazing features of Kotlin. We have barely scratched the surface, so keep checking back ;)
Now you are one step closer to becoming a pro app developer.
We at Terasol Technologies have great developers using these technologies. If you need a Kotlin Android app, do let us know!