Kotlin is a statically typed programming language for the JVM, Android, and the browser. Kotlin is a new programming language from JetBrains, the maker of the world’s best IDEs. Also, it’s now the official language for Android app development.
Why Kotlin ?
Before we begin highlighting the brilliant features of Kotlin, we need to understand how Kotlin originated and evolved. We already have many programming languages. How did Kotlin emerge to capture programmers’ hearts?
A 2013 study showed that language features matter little compared to ecosystem issues, when developers evaluate programming languages.
Kotlin compiles to JVM bytecode or JavaScript. It is not a language you will write a kernel in. It is of greatest interest to people who work with Java today, although it could appeal to all programmers who use a garbage collected runtime, including people who currently use Scala, Go, Python, Ruby, and JavaScript.
Kotlin comes from industry, not academia. It solves problems faced by working programmers and developers today. As an example, the type system helps you avoid null pointer exceptions. Research languages tend to just not have null at all, but this is of no use to people working with large codebases and APIs which do.
Kotlin costs nothing to adopt! It’s open source, but that’s not the point. It means that there’s a high quality, one-click Java to Kotlin converter tool (available in Android Studio), and a strong focus on Java binary compatibility. You can convert an existing Java project one file at a time and everything will still compile, even for complex programs that run to millions of lines of code.
Kotlin programs can use all existing Java frameworks and libraries, even advanced frameworks that rely on annotation processing. The interop is seamless and does not require wrappers or adapter layers. It integrates with Maven, Gradle, and other build systems.
It is approachable and it can be learned in a few hours by simply reading the language reference. The syntax is clean and intuitive. Kotlin looks a lot like Scala, but it’s simpler. The language balances terseness and readability as well.
It enforces no particular philosophy of programming, such as overly functional or OOP styling.
Kotlin Features
Let me summarize why it’s the right time to jump from native Java to Kotlin Java.
- Concise: Drastically reduce the amount of boilerplate code you need to write.
- Safe: Avoid entire classes of errors such as null pointer exceptions.
- Versatile: Build server-side applications, Android apps, or front-end code running in the browser.
- Interoperable: Leverage existing frameworks and libraries of the JVM with 100 percent Java Interoperability.
Brief discussion
Let’s discuss a few important features in detail.
Functional Programming Support
Functional programming is not easy, at least in the beginning. That is, until it becomes fun. With zero-overhead lambdas and ability to do mapping, folding, etc. over standard Java collections. The Kotlin type system distinguishes between mutable and immutable views over collections.
- Function purity
The concept of a pure function (a function that does not have side effects) is the most important functional concept, which allows us to greatly reduce code complexity and get rid of most mutable states.
2. Higher-order functions
Higher-order functions either take functions as parameters, return functions, or both. Higher-order functions are everywhere. You just pass functions to collections to make code easy to read. titles.map { it.toUpperCase()} reads like plain English. Isn’t it beautiful?
3. Immutability
Immutability makes it easier to write, use, and reason about the code (class invariant is established once and then unchanged). The internal state of your app components will be more consistent. Kotlin enforces immutability by introducing val keyword as well as Kotlin collections, which are immutable by default. Once the val or a collection is initialized, you can be sure about its validity.
Null Safety
Kotlin’s type system is aimed at eliminating the danger of null references from code, also known as ‘The Billion Dollar Mistake.’ One of the most common pitfalls in many programming languages, including Java, is that of accessing a member of null references, resulting in null reference exceptions. In Java, this would be the equivalent of a NullPointerException, or NPE for short.
In Kotlin, the type system distinguishes between references that can hold null (nullable references) and those that cannot (non-null references). For example, a regular variable of type String can’t hold null.
How to migrate effectively to Kotlin?
Migration is one of the last things that every developer or the organization wants. There are a lot of advantages when you migrate from Java to Kotlin, but the bottom line is, it will make the job of the developer easy, which in turn reduces bugs and improves the code quality and so on.
Migrating effectively will always have many routes. But my advice would be to first convince the management that you need to migrate (if you’re a developer). Then you need to start writing the test cases first, to get familiar with the language. Then, as Kotlin is of interoperable capacity, you can start changing one file/module at a time.
About the Author
Hari Vignesh Jayapalan is a Google Certified Android app developer, IDF Certified UI & UX Professional, street magician, fitness freak, technology enthusiast, and wannabe entrepreneur. He can be found on Twitter @HariofSpades.