Kotlin: Do more with less code


Java is the most widely used language for Android development, but that doesn’t mean it is always the best choice.The biggest problem with Java is that it is old, verbose, not a “modern language” and don’t forget the infamous NullPointerException.
Java 8, bringing some modern flavors to the language but Android certainly doesn’t use all the features of Java 8 and we are still stuck in the old Java 7 and 6.That's where Kotlin comes in: This relatively new open source language, based on the Java Virtual Machine (JVM), is gaining traction with Android software engineers.

What is Kotlin?

Kotlin is a statically-typed programming language(sometimes referred to as Swift for Android) that runs on the JVM, developed by the JetBrains team, and is now in its 1.1 version. What makes it useful in Android development is that it compiles to JVM bytecode. It is fully compatible with Java, and Kotlin code can be simply converted to Java code and vice versa (there is a plugin from JetBrains). That means Kotlin can use any framework, library etc. written in Java.

Why Kotlin

Java 8 solved some language issues and corrected even more with Java 10. In order to get much of a benefit from the corrections made in these two releases, you have to set the minimum SDK to Android 24 just to use Java 8, which isn’t an option for many developers. For almost everybody, Java 10 isn’t even on the radar.
Kotlin aims to fill that gap of a missing modern language for the Android platform. Kotlin has heap of features, here covering just the important ones to get you started.

No More NullPointerExceptions

One of the most common reasons for crashes in our apps have been the NullPointerExceptions.
Kotlin comes with null-safety system which helps in catching null(s) during compile time.
By default every variable is treated as non-null unless we tell to compiler that it’s a null variable using an special operator “?” at the end of the variable type.
Suppose you have a String variable and you are trying to assign a null value to it:


Using this null variable in further code will also give compile-time error unless handled using not null condition.


The compiler is smart enough to understand that we are using an object which can be null-able hence it shows compile time error.

Write Less Code, Be Happy- Lambda Expressions

A lambda expression represents an anonymous function. Lambdas are a great way of reducing the amount of code needed to perform some tasks that come up all the time in Android development.
Java 8 has introduced native lambda expressions, and these are now supported in Android Nougat. Although this feature isn’t something that’s unique to Kotlin, it’s still worth taking a look at them. Plus, if you’re working on a project that contains both Kotlin and Java code, then you can now use lambda expressions across your entire project!
Adding a click listener to a button. In Java 7 and earlier, this would typically require the following code:


However, Kotlin lambda functions allow you to set a click listener using a single line of code:


Smart Casting

Smart casting is one of the interesting feature of Kotlin compiler. The Compiler does smart casting on our behalf. It even handles the negative scenario like if the “view” is not an “TextView”.


Great IDE and Tooling Support

Kotlin is a JetBrains product, the ones who specialize in creating some of the best and most used IDEs in the world.
There is a plugin that you can install in Android Studio to get Kotlin working in your project.
Screen Shot 2017-04-30 at 11.25.07 AM-min.png
All IDE features work perfectly in Kotlin. You can mix and match Kotlin and Java code in the same project and everything still works great. The IDE support is really great with Kotlin where other JVM based languages don’t even come close.

About the Syntax

In order to understand the features of Kotlin. We will have a look at the basic syntax in Kotlin. You can refer the syntax in detail from the official website.

Variables

The way to define a variable is with the “val” or “var” keyword. The type will be inferred from the Context:


You can specify the type explicitly:


Did you notice there is no new keyword near ArrayList() ?. Because there is no new keyword in Kotlin.

Functions

Structure of the function:


  • Start with “fun” keyword
  • The parameter names are written before the type
  • The return type is written at the end of the function
The above function can be Kotlin-ized even more:

Did you notice there are no semicolons(;) ?. Kotlin is semicolon free language :)


To learn more about Kotlin, I recommend browsing the Kotlin reference. The Getting Started section will help you get up to speed with this new language.
In the next tutorial, you will learn how to use the Kotlin plugin and the Kotlin Android Extensions plugin for Android Studio.

Comments

Popular posts from this blog

How to Use Kotlin in Your Android Projects

Reactive Programming with RxBinding

Exploring Android O: Fonts in XML

Android Debug Database: A Library for Debugging Android Databases and Shared Preferences

Exploring Android O: Autosizing TextViews

Introduction to Android Bottom Navigation View

Introduction to Spring Animation

Getting started with Android Fingerprint Authentication