Say Hello to Kotlin

Say Hello to Kotlin

Hello reader, I am here to talk about Kotlin, Kotlin is a Russian island, located near the head of the Gulf of Finland in the Baltic Sea, and also a programming language named after that island.

Kotlin is a statically typed, open-source, general-purpose programming language developed by JetBrains. It was first announced in 2011 and Kotlin 1.0 was released in 2016. It targets the JVM, Android, JavaScript, and Native, so Kotlin can be used everywhere where Java works. Kotlin also targets front end web development with Kotlin Script, and with Kotlin Native the developer doesn’t have to master another language(JS/Dart in case of ReactNative/Flutter) one can write code in Kotlin and build cross-platform apps that will run in both iOS and Android with their Native UI.

Now one would think why Kotlin? when we already have Java with all of its legacy, frameworks, and libraries. I’ll say there are many reasons(stay tuned for a detailed answer) for now just know that all of your existing Java code and libraries will work perfectly fine with Kotlin too, as Kotlin is fully interoperable with Java.

“Hello, World!” in Kotlin:

Hello World in Kotlin

Notice the code conciseness here, Kotlin tries to reduce the number of lines of code required to accomplish a task. And as we know the number of bugs is directly proportional to the number of lines of code, the Industry average is 15 to 50 bugs per 1000 lines.

JetBrains claims that Kotlin can reduce about 40% of lines of code compared to Java, this improves code maintainability and readability. And one more thing, here semicolons are not required to mark the end of the line for statements. 

Kotlin for Android Development is better than Java(as claimed by many), Let’s understand why?

Google made Kotlin an official language for Android development: Google announced in Google I/O conference 2017, that it will officially support Kotlin on Android as a “first-class” language. Since then all Android tutorials(offered by Google) and Android documentation are available in Kotlin with JAVA. App development for Android in Kotlin is nearly flawless hence companies like Pinterest, Uber, Evernote, Kickstarter were one of the first to adopt Kotlin in the industry.

The Billion dollar mistake: Tony Hoare(Sir Charles Antony Richard Hoare) in Qcon London conference 2009, apologized for creating Null Pointer reference. He said, “Null reference has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

The creators of Java adopted Null as the default value for any variable/object. Kotlin tries to solve this Billion dollar problem by making all objects of non-null type by default. To assign Null value to any object you’ll have to make it nullable using a special symbol(?)

Say Hello to Kotlin

and once you do that, that object becomes available for the compile-time check which means if your application is going to crash because of NullPointerException you’ll know it in compile-time itself, not in production. Kotlin also has some special keywords (lateinit and by lazy) to delay the initialization of an object and avoid assigning Null.

Data classes in Kotlin: Data classes make the code look clean and concise and avoids a lot of common boilerplate code. data class is a keyword in Kotlin which is used to create classes whose main purpose is to hold data. This generates all required getter, setter methods and overrides toString() , hashCode() and equals() all by itself.

Say Hello to Kotlin

Extension Functions: An extension function is a member function of a class that is defined outside the body of the class. One can use Class_name.method_name() to inject a method into the class body, you can write this in any file inside your project where that class is accessible.

Kotlin’s compiler: Kotlin’s compiler catches errors, mistakes such as NPE which one would only encounter while testing or in production. Kotlin’s compiler targets to fail fast, it performs many checks avoiding runtime errors and hence reduces the cost of error and fixes.

Where Kotlin lags behind?

Kotlin is a new language and Kotlin community is very young hence resources are also limited, finding an experienced guy to mentor your team is a little difficult. Kotlin takes more time to compile than Java in the majority of cases. If one is familiar with Java they can pick Kotlin easily, Kotlin’s concise syntax is an advantage but it requires some learning to understand what all is going on behind the curtains.

To sum it all up Kotlin code is concise, less prone to NPE, type-safe, and better overall but what all of this mean for you? If you want to learn Kotlin or write your existing code in Kotlin, I’ll say start slow, pick one small file/class which you think will be easy and write that in Kotlin as you’ll go forward you’ll understand what Kotlin has got for you in the store.

Kotlin is not going to replace Java anytime soon but it is here to stay as a better alternative to Java and personally, I believe that Java and Kotlin both can go hand in hand in a project as Kotlin is fully interoperable with Java.

Thanks for reading… Happy coding mate 🙂

About the Author

DevLabs Expert Group

DevLabs Expert Group

Related Posts