Learn Kotlin Programming

Learn Kotlin Programming

Kotlin is a Statically typed programming language. It runs on the Java virtual machine and also can be compiled to JavaScript source code. It is developed for modern multi-platform applications.

 It is safe, Interoperable, concise and easy to read and write.

kotlin-programming

In this guide, you find information on:

  • Why learn Kotlin language?
  • How to get started with Kotlin?
  • How you can learn Kotlin?
  • Build Applications For Platforms

Why Learn Kotlin language?

You can learn Kotlin language for following  reasons.

1. Safe

  • Forget about NullPointerExceptions.
var output: String
output = null   // Compilation error
  • Kotlin protects you from mistakenly operating on nullable types
val name: String? = null    // Nullable type
println(name.length())      // Compilation error
  • And if you check a type is right, the compiler will auto-cast it for you
fun calculateTotal(obj: Any) {
    if (obj is Invoice)
        obj.calculateTotal()
}

2. Interoperable

  • Use any existing java library on the JVM, as there’s 100% compatibility.
  • Target either the JVM or JavaScript.
  • Write code in Kotlin and decide where you want to deploy to
import kotlin.browser.window
fun onLoad() {
window.document.body!!.innerHTML += "<br/>Hello, Kotlin!"
}

3. Concise

  • Implement code with easy and simple syntax and using few lines of code.
  • Develop complex functionality using simple code.
  • Create a POJO with getters, setters, equals(), hashcode(), toString() and copy() in single line:
data class Customer(val name: String, val email: String, val company: String)
  • Or filter a list using a lambda expression:
val positiveNumbers = list.filter { it > 0 }
  • Want a singleton? Create an object:
object ThisIsASingleton {
    val companyName: String = "JetBrains"
}

How To Get Started with Kotlin?

Assuming following prerequisite is installed on your computer.

  1. IntelliJ IDEA
  2. Java

Get started and follow below steps to create Kotlin project and run it on your computer.

  • Open IntelliJ IDEA
  • Click Create New Project

 

create-kotlin-project

  • On the left menu, select Kotlin > Kotlin (JVM) and hit Next

create-kotlin-project-jvm

  • Now, give this project a name: “HelloWorld” and select project location for this project and click finish.

create-kotlin-project-setup

  • You should see your project on the left menu. If you can’t see this window, go to View > Tool Windows > Project
  • Now create a new Kotlin file: Hello > New > Kotlin File/Class

create-kotlin-project-file

  • A pop up window will appear where you can provide name for the Kotlin file. We will name it main. Then, click OK.
  • You can see the file main.kt
  • kt is the file extension for Kotlin files.
  • Now, write below code in this file and save it.
fun main(args : Array<String>) { 
  println("Hello, world!") 
}
  • To run it, go to Run > Run

run-kotlin-program

When you run the program, the output will be:

Hello, world!

 

How you can learn Kotlin?

You can learn Kotlin from tutorials available on this site. Checkout official site for more updates.

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.

Build Applications For Platform

You can build applications for following platforms using Kotlin language.

  1. JVM
  2. Android
  3. Browser
  4. Native

You can find more tutorials here.

SmartBit Infotech

Software company in Pune, Maharashtra. Offers training on leading technologies in market.