close

Learn Kotlin - Tutorial for Beginners

The Definitive Guide

Kotlin is a programming language developed by JetBrains for modern multiplatform applications.

It is safe, concise and fun to read and write.

In this comprehensive guide, you find information on:

  • Why learn Kotlin?
  • How to get started?
  • How you can learn it?

We have also created an infographic that gives an overview on Why learn Kotlin, it's present and future. Click the image below for full view.

Why learn Kotlin? (Infographic)

Things to Know Before Learning Kotlin

https://www.programiz.com/

Why Kotlin was Created?

Kotlin was created by JetBrains, a company renowned for creating development tools for professionals.

Back in August, 2011, JetBrains published a blog post, Why JetBrains needs Kotlin, explaining reasons behind the creation of Kotlin. Here are the main points:

  • To increase their (JetBrains) productivity
    Back then, JetBrains team were writing all of their IntelliJ-based IDEs almost entirely in Java. It took a lot of time to compile with javac. They wanted to increase productivity by switching to a more expressive language.
  • To drive sales of IntelliJ IDEA
    They wanted the enterprise development frameworks and tools for Kotlin to be the part of IntelliJ IDEA Ultimate (commercial version) thereby increasing its sales.
  • Drive company's business by keeping trust
    JetBrains is trusted by a lot of professional developers. They wanted to drive company's business as well as attract more people to their approach of developing tools by increasing community awareness of JetBrains and maintaining the trust.

Features of Kotlin

Kotlin is a statically typed language. Here are some of the important features of Kotlin:

  • Open source
    Kotlin is distributed under Apache License, Version 2.0. The Kompiler (Kotlin compiler), IntelliJ IDEA plugin, enhancements to basic Java libraries and build tools all are open source.
  • Interoperable with Java and Android
    It is 100% interoperable with Java and Android. This means all your current Java/Android code works seamlessly with Kotlin. For more information, visit: Mixing Java and Kotlin in One Project.

    In fact, Android team announced Kotlin a first class language for developing Android apps.  In addition to that, it allows you to compile quite a few Kotlin projects to JavaScript modules.
  • Concise and Expressive
    Rough estimation indicates that, using Kotlin allows you to cut off the lines of code by approximately 40% (compared to Java).

    Expressive means, it's easy to write code that both humans and compilers can understand easily.
  • Easy to Learn
    It is influenced by Java, Scala, Groovy, C#, JavaScript and Gosu. Learning Kotlin is easy if you know any of these programming languages. It is particularly easy to learn if you know Java.
  • Tool-friendly
    Kotlin is developed by JetBrains, a company renowned for creating development tools for professionals. No wonder, it is tool-friendly.

    You can choose any Java IDE to run Kotlin or build it from the command line.
  • Safe
    It is aimed at removing dangers of null references from the code (a big headache in Java). Also, it is statically typed which gives ability to catch more errors at run-time (type safe).

The Present and Future of Kotlin

https://www.programiz.com/

The Present

  • Many companies like Netflix, Uber, Trello, Pinterest, Corda etc. are using Kotlin (along with other programming languages) to create applications.
  • The number of lines of code increased from 2.1 million to 10 million in an interval of year from 2016 to 2017.
    Growth of Kotlin in Github
  • At recent Google I/O held on 17 May, 2017, Android team announced Kotlin as an official language for Android app development.
  • You can use replace Java code with Kotlin seamlessly. It is 100% interoperable with Java and Android.
  • Though the majority of progress has been made on Java and Android, it's not a Java/Android language. It can compile many Kotlin projects to JS. Similarly, Kotlin/Native is under progress that compiles Kotlin directly to machine code.

The Future

The future plans include the concept of common module in Kotlin. It allows you to develop platform specific functionality for multiplatform development.

How?

Suppose, you are trying to create a mobile app for iOS and Android. For that, you need to use Java, Kotlin programming etc for Android development, and swift, objective-C etc. for iOS development (present situation).

In future, you will be able to write platform specific code (in Kotlin for Android and iOS app) in the implementation module. And, the common module includes common API's and implementation headers (without implementation). This allows you to create both iOS and Android app written in Kotlin.

According to Andrey Breslav, lead language designer and project manager of Kotlin, they already have working prototype of it and shouldn't be very far. However, they are not promising anything yet until the feature is released.

Possible Future Projects in Kotlin

  • Cross-platform game development
  • Cross-platform mobile application development
  • Server-side and microservices
  • Data analysis and machine learning
  • Embedded system: Arduino/Raspberry Pi to professional controllers directly.

Run Kotlin in Your System

https://www.programiz.com/

Assuming Java is installed on your computer, it's pretty easy to run Kotlin.

Visit this page to run Java on your computer if you haven't already.

You can use any popular Java IDE to run Kotlin. The most popular among them is IntelliJ IDEA (Community Edition). The latest version of Intellij IDEA fully supports Kotlin without any plugin. Just download Intellij IDEA, open it and follow the instructions to install it.

To run Kotlin in Eclipse, you need to install Kotlin plugin. Visit this page to learn more on how to run Kotlin with Eclipse Neon?

Assuming you are using IntelliJ IDEA, here are the steps you need to follow to create a project and run Koltin.

  1. Open IntelliJ IDEA.
  2. Click Create New Project.
    IntelliJ IDEA start page
  3. On the left menu, select Koltin  > Kotlin (JVM) and hit Next to work with Kotlin/Java.
    Create Kotlin project in IntelliJ IDEA
  4. Now, give this project a name and select location for this project and hit finish. I have named it Hello.
  5. You should see your project on the left menu. If you can't see this window, go to Views < Tool Windows < Project
  6. Now go to Hello > New > Kotlin File/Class to create a Kotlin file.
    Create Kotlin file in intellij IDEA
  7. A pop up window will appear where you can provide name for the Kotlin file. We will name it main. Then, click OK.
  8. You can see the file main.kt. kt is the file extension for Kotlin files.
  9. Now, write your code in this file and save it (Ctrl + S). For Mac, it's Cmd + S. To run it, go to Run < Run...
    Run Kotlin in IntelliJ IDEA

Your First Kotlin Program

https://www.programiz.com/

Often, a program called "Hello, World!" is used to introduce a new programming language to beginners. A "Hello, World!" is a simple program that outputs "Hello, World!". You will learn to write the same program in Kotlin.

fun main(args : Array<String>) { 
  println("Hello, world!") 
}

When you run the program, the output will be:

Hello, world!

If you are familiar with Java, the above code is equivalent to

class HelloWorldKt {
    public static void main(String[] args) {
        System.out.println("Hello, World!"); 
    }
}

Visit this page to learn more about how Kotlin Hello World program works.

How to learn Kotlin

https://www.programiz.com/

Learn Kotlin from Programiz (Work in Process)

We are working hard to create Kotlin tutorials as well as online Kotlin compiler to test Kotlin programs.

The task should be completed in a copule of months. Subscribe now and you will be the first one to know once the tutorial is complete.

The Kotlin Website (Official Site)

Kotlin reference and Kotlin tutorial from official site is the most reliable source to learn Kotlin. The official reference is continuously updated to keep up with changes to the Kotlin. When in doubt, always follow official resources.

Recommended Books

Here's a list of books to learn Kotlin.

Other useful Resources

Here are few resources to expand your Kotlin knowledge, and seek help when stuck.

Kotlin has already gained reputation for being concise, expressive and safe. And, with Google's announcement (an official language for Android development), it's safe to say that Kotlin is here for the big run.

If you are a Java/Android developer, we recommend you to learn Kotlin when possible. It not only makes your CV better but also helps to increase productivity.