Android App Development
Why should we use Kotlin for Android app development?January 3, 2022
Only In 2017, Google officially accepted Kotlin as the second official language for Android app development. At the end of the second quarter of 2019 Google has declared that Kotlin has been preferred as a programming language for Android app developers. Popularity of the Kotlin has increased overwhelmingly in the past two years. According to Google, more than 50% of professional Android developers currently use the language to develop apps, and it is the fourth most popular programming language according to the latest Stack Overflow developer survey. But what is Kotlin, why does Google suggest Kotlin as the preferred language? Let’s dig it!
What is Kotlin?
For kotlinlang.org, “Kotlin is a modern but already mature programming language aimed to make developers happier. It’s concise, safe, interoperable with Java and other languages, and provides many ways to reuse code between multiple platforms for productive programming.”. Basically, it is an object-oriented programming language (like Java 🙂 ) that also allows functional programming. It is designed by JetBrains teams who created the IntelliJ IDEA , and now you will probably guess that Kotlin is fully compatible with Java of course 🙂 You can use any java libraries in your Kotlin project with clarity, tooling support, safety and interoperability. Unlike Java, you can code iOS applications using Kotlin.
Hmm ok, but what is the differences between Java and Kotlin, and why is Kotlin better than Java?
According to the official Kotlin web site you can clearly see what Kotlin has that Java does not (https://kotlinlang.org/docs/comparison-to-java.html#what-kotlin-has-that-java-does-not). As an Android developer, syntax going to be the first thing that comes to my mind. We can save lots of lines of code while working with Kotlin. Let me explain basically. If you are writing Data Transfer Objects in Java, your code probably looks like similar to hashCode, equals, toString, getters, setters and constructors. For simple objects like for example Course with id, name, capacity, email, schoolName you will have approximately 70+ lines of codes as shown below.
How will this “Course” object look in Kotlin?
It takes only a single line of code to write Course DTO object in Kotlin with more transparent and easier to understand.
I believe that according to many developers concision should never take precedence over readability. Boilerplate code is difficult to read, results with more bugs and takes more time attempting to find them.
Migration (Java to Kotlin)
In Android projects you can have both Kotlin and Java languages simultaneously. This means that you can migrate your Java project to Kotlin easily with only small steps.
Smart type casting
In most circumstances, you don’t need to use explicit cast operators in Kotlin since the compiler keeps note of is-checks and explicit casts for immutable values and automatically inserts (safe) casts as needed; What I am trying to say is that Kotlin compiler is tracking code logic and automatically cast type object for example:
Lambdas and high-order functions
With Kotlin,you can use improved version of Java Lambdas. Kotlin functions can be saved in variables and data structures, as well as passed to and returned by higher-order functions. (https://en.wikipedia.org/wiki/First-class_function)
Let me give you a simple example of a high-order function mapMutable. It takes transform values and applies the given transform function to each element of the original array and appends the results to the given destination, returns as mutable list.
Assume that you want to show a menu list in your application. Firstly you need to create menu list item data class as given below (you can change variables when needed)
Second, you need to determine which item types will be in your list.
Last but not the least, if we want to show that menu we need to create generic list adapter using recycler view in our xml file. However; I won’t show you how to do it right now because this will be our another section of reading.
Now, I will show you how can you use your mapMutable object in that list , we have interface function to show the list of our main menu items.
Additionally, we have setDefault function to set the list in our onViewCreated function when class rendering we want to set list as default.
Inbuild Null Safety, No raw types, No checked exceptions
As an Java or Android developers we all encountered with Null Pointer Exception and this exception can easily destroy the applications. (https://en.wikipedia.org/wiki/Tony_Hoare#Apologies_and_retractions ) Kotlin solves this issue by incorporating inherent null safety. This feature eliminates the need for developers to write additional code to deal with the problem. (https://kotlinlang.org/docs/null-safety.html#safe-calls)
Raw types can cause a CastClassException, and the fault will occur during execution rather than compilation.Kotlin does not support raw types, it produces more type-safe code.
Checked exceptions are often unnecessary and cause empty each blocks (as always actually 🙂 ). Developers don’t like non-existent checked exceptions because empty catch blocks force them to search through code to find a non-existent exception. As a result, Kotlin eliminates them entirely, reducing verbosity and improving type safety.
Is the end of Java coming?
Of course NOT!
Java is a well-known programming language that comes with a plethora of open source tools and libraries to aid programmers. As a matter of fact, the language is not perfect , and even Java has issues that can make developer’s job more difficult. Kotlin helps solving common programming problems while improving the Java ecosystem as a whole. Kotlin has evolved into a more stable and consistent development alternative for Android Studio during the last four years. As all developers guessed in 2017, Kotlin supplant Java as the preferred language for Android Development. Even today many companies make their projects with Kotlin and Java at the same time, they think that they can be coexist without one overpowering the other.
Author: Erdi Koç