When Google announced Kotlin as a first-class language for Android development, the development community exploded with excitement. But what is so great about Kotlin? And why should you learn it?
This article will help answer those questions and point out 8 ways that Kotlin makes your life (as a developer) better:
We’ll also show you how to get started with Kotlin by introducing you to an app developed entirely in the language called Lightweight Dispatcher.
1. Kotlin is 100% interoperable with Java
One of the biggest concerns when considering using another programming language is whether or not it can integrate well with the existing code base. Fortunately, this isn’t something you have to worry about with Kotlin because it’s 100% interoperable with Java, which is the standard language for Android development.
2. It’s statically typed
Generally speaking, a statically typed language is one in which variables have to be declared before they can be used and each variable has a specific type that can’t be changed. Variables in Kotlin are declared using the data keyword. For example, this code shows how you would declare String and Int types:
Data class Example (var name: String)
Because of its static typing, Kotlin is able to perform certain optimizations at compile time that increase performance while reducing memory usage.
3. It has null-safety built into the type system
Have you ever seen an exception like this before? “java.lang.NullPointerException” The dreaded null pointer exception is a type of software failure that occurs when trying to dereference (or access) memory that has not been initialized or that has been deleted/made read-only.
Kotlin helps solve this issue by making all types nullable and adding an extra level of safety with something called the!! Operator. For example, you would use the following code to both declare and initialize a variable:
4. It has excellent standard library functions
5. It makes working with collections much easier than Java
One of the most common tasks performed by mobile app developers is data manipulation using collections (also known as lists). Kotlin provides several useful functions for working with different types of collections. For example, if you wanted to get the last element from a list it would be as easy as:
val list = arrayListOf(“one”, “two”, “three”, “four”)
list.last() // => four
6. Its more concise than Java
In general, one of Kotlin’s goals is to reduce code size while still preserving readability so the code you write will often be more compact than what you would have written in Java. This is especially true for collection manipulation since the above example only took 5 lines of code!
7. It enforces immutable variables by default
In Kotlin, variable reassignment (including passing them to functions) results in an error unless the variable is declared as “mutable” using the mutable keyword like this:
var name = “Jon” //name can now be reassigned
mutableName = “Josh” //allowed; however, name still cannot be reassigned (no compile-time error)
Another benefit to declaring variables as mutable or immutable is that it prevents possible race conditions that occur if two separate parts of a program are trying to change the variable at the same time. You can take the assistance of the DBA administrators.
8. It makes use of extension functions and properties by convention
Kotlin is definitely worth learning! It’s seen some great success lately (especially with the release of Android Studio 3.0) and I can’t wait to see what they come up with next.