Introduction of Kotlin programming language
Tags: kotlin, java, android, programming
Published: 2019-07-13 10:47:35

---
title: Introduction of Kotlin programming language
published: true
description: Introduction of Kotlin programming language
tags: kotlin, java, android, programming 
cover_image: https://thepracticaldev.s3.amazonaws.com/i/4y89qk4qr8jl8crlwtb3.png
---

The article starts with a question, what is the most painful problem that every Java developer faces? I know most of you will say *“The Null Pointer Exception"*. While writing any software program, we end up with lots of null checks. If you forget the null check, your application will crash. Though Java 8 introduced optional but instead of making life easy, they put more burden on programmers i.e. first wrap actual value in a wrapper then checks of `isPresent` and so on. Shouldn't be a programming language where handling those exceptions could be easier and more readable? 

The answer is **Kotlin**.

Kotlin is a statically typed JVM language developed by Jetbrains in 2011.
- It is interoperable fully with Java, and the JVM version of its standard library depends on the Java Class Library.
- It mainly targets the JVM, but also get transpiled to JavaScript or native code.

# Benefits of Kotlin
Kotlin is the modern language which makes development more delightful. There are following benefits of using Kotlin-
- **Concise-** It is concise, that means you can *"do more with less code"*. As a result, your code will look more beautiful and expressive. Read article on [Importance of writing clean code].(http://blogs.ourtechnobytes.com/2017/06/importance-of-writing-clean-code.html)
- **Enforces Best Practices-** While declaring a field, you have to specify **val** or **var** before it to decide whether it is mutable or immutable. By default every class in the Kotlin is `final`. To allow inheritance, you have to specify a class as `open` explicitly. Kotlin enforces developers to use best practices.
- **Has both OO and FP style-** Kotlin is not fully object-oriented language. You can use it as object oriented or you can write your code without using classes also.
- **Interoperable with Java-** Java code can be used in Kotlin and Kotlin code can be used in Java. Also, it's easy to convert Java code to Kotlin using Android Studio or various online converters. To use Kotlin in existing Java project, you don't need to rewrite everything from scratch. You can start writing new classes in Kotlin and if needed, can convert existing Java code to Kotlin gradually.
- **User Friendly Keywords-** Kotlin replaced `switch` statement with `when`. Similarly introduced `is` instead of `instanceof`. Kotlin comes with real life keywords which makes program more expressive.

# Interesting Features
Now look at the some cool features of Kotlin-

- **Easy Null Pointer Handling-** As discussed initially, Kotlin provides easy `NullPointerException` handling without using too many null checks. If there is any chance of null pointer exception, Kotlin compiler will give you error at compile time. It provides safe-calls, non-null assert and elvis operator to deal with null values. I will write a complete article on handling null pointer with Kotlin and its comparison with Java and Optional.

- **Smart Cast-** Let's look at below example-

Type casting in Java-
```java
if (person instanceof Employee && ((Employee) person).isActive) {
   return ((Employee) person).salary;
}
```

Type casting in Kotlin-
```kotlin
if (person is Employee && person.isActive) {
   return person.salary;
}
```

Cool!!! If you have already checked `person` is the instance of `Employee`, you don't need to cast it again and again. This is called smart cast.

- **Data Classes-**- To create POJO or Model or Data Holder classes, you do not need to write lots of boilerplate code i.e. `getters/setters`, `toString()`, `equals()`. Though Java has libraries like Lombok for this purpose, Kotlin comes with first class support of data classes.

- **Easy Singleton**- We will see the singleton with below example-

Singleton class in Java-

```java
public class MySingletonClass {
    private static MySingletonClass mySingletonClass;

    private MySingletonClass() {
        // your code
    }

    public synchronized MySingletonClass getInstance() {
        if (mySingletonClass == null) {
            mySingletonClass = new MySingletonClass();
        }
        return mySingletonClass;
    }
}
```

Same singleton class in Kotlin-

```kotlin
object MySingletonClass{
// your code
}
```

Make a class `object` and you don't need to write boilerplate code to create a singleton class.

- **Named Arguments-** If you look at below method call, you will not know what each argument is doing-
```java
textView.setCompoundDrawables(image, null, null, null);
```

In Kotlin, you can provide names to the arguments as follows-
```kotlin
textView.setCompoundDrawables(left = image, top = null, right = null, bottom = null);
```
Now this method call is more descriptive.

- **Domain Specific Language(DSL)-** You must have seen below code in the gradle based projects-

```groovy
dependencies{
 implementation 'com.otb.utility:preference:0.0.1'
}
```

It's called DSL where you can write code in english like syntax. You can make same method calls in Kotlin. There are plenty of in-built and third party libraries which provides DSL i.e. String Spec for unit test cases.

- **Internal Access Specifier-** Java has several access specifier i.e. `public`, `private`, `protected` and default. Suppose you're developing an SDK and don't want SDK users to access that class, what will you do? You cannot make it `private` or default because it is using in other package of your SDK code. If you make it `public` then it can be used from SDK client. Kotlin introduced new access specifier `internal`. An internal class/field can be access within a module only.

Apart from these features, Kotlin has lots of more features i.e. more descriptive and easier functional programming, nested functions, typealias, inline functions, extension functions etc. You can find more features and syntax at [official site](https://kotlinlang.org/) of Kotlin.


# Where to Use
Kotlin is a language which can be used to create Android applications, you can create native library which can be used in multiple platforms. You can write back-end code. You can also create desktop applications with it. Let's see the popularity of Kotlin and famous companies which are using it-

- It is used by Google, Amazon Web Services, Pinterest, Coursera, Netflix, Uber, Square, Trello, Basecamp, Atlassian and others.
- Corda, a distributed ledger developed by a consortium of well-known banks (such as Goldman Sachs, J.P. Morgan, HSBC etc), has over 90% Kotlin in its codebase.
- Prezi - A presentation software company using it in backend which has over 100 million users
- You can use Kotlin with all popular IDEs i.e. IntelliJ IDEA, Android Studio, Eclipse, Visual Studio Code etc.
![Popular IDEs](https://raw.githubusercontent.com/mohitrajput987/media-repository/master/kotlin/ide-image.png)


# Gaining Popularity
Kotlin has quickly skyrocketed in popularity-
- Google declares Kotlin as the primary language to develop Android apps in Google IO 2017.
- It was voted one of the five most loved languages according to [Stack Overflow](https://stackoverflow.com/questions/tagged/kotlin).
- It is the fastest growing programming language according to [GitHub](https://github.com).

This article doesn't have intention to hurt Java developers(as I am also a Java developer and its big fan) but it's comparison is easy with Java i.e. how you can write same code with less number of lines. I hope this article is sufficient for the introduction of Kotlin. I will write detailed articles for each feature. 

Read more at my official site [Our Techno Bytes](http://ourtechnobytes.com)