DEV Community

Cover image for Introduction of Kotlin programming language
Mohit Rajput
Mohit Rajput

Posted on • Updated on

Introduction of Kotlin programming language

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-

if (person instanceof Employee && ((Employee) person).isActive) {
   return ((Employee) person).salary;
}
Enter fullscreen mode Exit fullscreen mode

Type casting in Kotlin-

if (person is Employee && person.isActive) {
   return person.salary;
}
Enter fullscreen mode Exit fullscreen mode

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-

public class MySingletonClass {
    private static MySingletonClass mySingletonClass;

    private MySingletonClass() {
        // your code
    }

    public synchronized MySingletonClass getInstance() {
        if (mySingletonClass == null) {
            mySingletonClass = new MySingletonClass();
        }
        return mySingletonClass;
    }
}
Enter fullscreen mode Exit fullscreen mode

Same singleton class in Kotlin-

object MySingletonClass{
// your code
}
Enter fullscreen mode Exit fullscreen mode

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-
textView.setCompoundDrawables(image, null, null, null);
Enter fullscreen mode Exit fullscreen mode

In Kotlin, you can provide names to the arguments as follows-

textView.setCompoundDrawables(left = image, top = null, right = null, bottom = null);
Enter fullscreen mode Exit fullscreen mode

Now this method call is more descriptive.

  • Domain Specific Language(DSL)- You must have seen below code in the gradle based projects-
dependencies{
 implementation 'com.otb.utility:preference:0.0.1'
}
Enter fullscreen mode Exit fullscreen mode

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 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

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.
  • It is the fastest growing programming language according to GitHub.

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

Latest comments (7)

Collapse
 
siddhantkcode profile image
Siddhant Khare

Thanks for sharing this information !

Collapse
 
avdheshsolanki profile image
Avdhesh Solanki

Amazing article to learn basics about Kotlin.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Thanks for your valuable feedback. Keep reading.

Collapse
 
openlowcode profile image
Nicolas de Mauroy

Hi,

by using Kotlin, do I get full access to the java APIs and libraries ? To quote just two examples of what I use in my open-source project ( Open Lowcode ), could I use Apache PDF Box and javafx ?

This was not clear to me reading your articles.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Yes, you can access any java code from Kotlin, including third party libraries.

Collapse
 
lankydandev profile image
Dan Newton

The JVM integration is one of the best things about using languages like Kotlin and Scala. Years and years of well tested libraries that just work.

Also like the Corda shout-out as one of the engineers that works on it πŸ˜€πŸ˜€.

To see usage of Kotlin in large codebases it is probably a good place to start. Also taking a look at libraries like Ktor that are also developed in Kotlin is a good idea!

That being said, I am curious how java turns out as it takes in newer features that other languages have adopted. Personally I'm still on java 8 though πŸ˜‚πŸ˜‚.

Collapse
 
mohitrajput987 profile image
Mohit Rajput

Indeed JVM is the powerful VM on which many languages run. Start using Kotlin for some new features and share if you see any changes in the code readability and LOC.