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.
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.
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.
– Kotlin entered the room.
Another fancy-named language with 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 Kotlin to eliminate this from ground-up. Which is why even the basic syntax is built to prevent this. By default, every variable you declare needs to initialized at that very moment with a value
var link = "http://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 Kotlin 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 against 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 right in front of you!
No! Kotlin > Java
Kotlin does not require a semicolon(;) after every statement, unlike Java. Which not only means faster typing, but also a very little but significant decrease in size.
And one of my most favorite things about Kotlin 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 become a pro app developer. And don’t forget, an astonishing app also requires a good user interface to go with it. Give that a read too!
If you need a Kotlin Android app, do let us know!
- Android changed its way of walking, but would it hit your app? - October 21, 2019
- Visitor Management System are they important? - September 10, 2019
- 5 UX laws to increase the user engagement of your mobile app – Part 1 - September 2, 2019