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
Kotlin’s standard library contains many useful functions that make developing in Kotlin fun and enjoyable. Some examples of these are the let () function that lets you easily declare two or more variables on the same line (as well as nested “let”s within each other), the apply() function which provides an easy way to call a function with another function as an argument, and the run() function which is similar to the “dot” notation used by languages like JavaScript so you can quickly access properties inside objects.
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
The methods that can be called on lists (and other collection types) are called extension functions because they extend the functionality of an existing type without modifying that type in any way. This makes them very similar to Higher-Order Functions which exist in many other programming languages such as JavaScript and Java 8.
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
If you’ve used other modern programming languages such as Java 8 or newer versions of Kotlin, JavaScript (ES6), or C#, then you probably saw the concept of static methods versus instance methods in classes before. By default, when creating a new class in Kotlin you define regular instance methods on that class itself so it’s more intuitive when using them in your code compared to using static methods in most other statically typed languages.
Conclusion
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.