Kotlin and Java — Initial thoughts!!
--
Hey Folks! I recently started on a project which is implemented in Kotlin. They built this project sometime back, and I was curious about the choice of Kotlin. I started researching about what Kotlin brings to the table and also why Kotlin, as a programming language, is gaining steam. In this post, I will talk about some advantages of Kotlin over Java and also about why experienced developers prefer building a new project in Kotlin over building it in Java.
Kotlin, as Java, is a JVM based programming language and is fully inter-operable with Java. JVM based programming brings in the standard features such as autonomy in the choice of platform, support for security sand boxing to prevent the program from directly interacting with OS resources (reading/writing to hard drive, opening network connections). Memory management as garbage collection to claim unused memory or maybe allocation / de-allocation of memory to prevent problems such as memory leak are some of the most extensively valued features of a JVM. Kotlin brings in all these features besides its already rich feature set. Kotlin is a statically typed language unlike python, which is dynamically typed.
The major feature IMO Kotlin has over Java is the built-in support for Coroutines. If the main program needs to perform some lengthy network I/O and CPU intensive operations, programming languages such as Java provides the support for spawning multiple background threads. Managing different threads is a complex task and makes the code error prone. Another not-so-cool way of performing these operations is running them on the same thread, which results in blocking off the main thread until the completion of the entire operation. Kotlin solves this problem by providing the developers the support for launching such coroutines.
We can use Coroutines as a weapon in the battle of Asynchronous programming. These are very similar to Goroutines. But coroutines unlike go routines don’t provide the support for concurrency. Coroutines can’t communicate with each other and kotlin doesn’t provide an in-built support for switching contexts between different OS threads. Stack full coroutines are more general purpose and powerful than stackless coroutines. A stack less coroutine can only suspend itself from a top level function and therefore the code execution must finish before suspending the coroutine. Hence, stackless coroutines have a lower memory footprint and are efficient. In comparison, we can suspend a stack full coroutine at any nested depth of the call stack and therefore, all functions called from the coroutine must finish before suspending the coroutine. This makes stackful coroutines more powerful and general purpose than stackless coroutines.
Kotlin’s type of system also boasts of in built null safety. Java developers have had a long-standing problem of facing NullPointerExceptions. These are frustrating as it allows the assignment of null to variables. Access to such variables raises a null pointer exception and needs handling. Kotlin addresses these issues, as all variables are non-nullable. Kotlin will throw compile-time errors if variables are assigned null values and, on top of it, kotlin also provides support for explicitly declaring variables where we want to have null values. Sample Code:
value num: String ? = null
Another major feature in the arsenal of kotlin is the support for Data Classes. Data classes are the classes which hold data and have minimal (almost zero) functionality. A simple addition of a keyword “Data” in the class definition will make the compiler auto-generate the constructor / the getter & setter functions for several fields and variables.
With the use of kotlin you can write way less code. Kotlin’s conciseness helps in the reduction of chances of making code errors. It makes your life as a developer simpler and also helps in making larger projects simpler. Brevity, in terms of fewer lines of code for same functionality, is something which is new for java developers and helps in following the KISS principle without compromising the readability of the project. KISS is one principle which I am an ardent believer in and working for AWS has reinforced my faith in it.
I ‘ll follow this principle for this post as well and will end it. Signing off for now! Ciao — AD