Kotlin Tutorial
Kotlin is a statically typed programming language that runs on the Java Virtual Machine. It is known as the Swift of the Android world, designed, developed, and open-sourced by JetBrains.
Kotlin can be compiled into Java bytecode or JavaScript, making it convenient to run on devices without a JVM.
At Google I/O 2017, Google announced that Kotlin became the official development language for Android.
My First Kotlin Program
Kotlin program files end with.ktthe extension, such as: hello.kt, app.kt.
Minimal Version
package hello //Optional package header
fun main(args: Array<String>) { //Package-level visible function that accepts a string array as a parameter
println("Hello World!") //Semicolons can be omitted
}
Run Example »
Object-Oriented
class Greeter(val name: String) {
fun greet() {
println("Hello, $name")
}
}
fun main(args: Array<String>) {
Greeter("World!").greet() //Creating an object does not require the new keyword
}
Run Example »
Why Choose Kotlin?
- Concise: Greatly reduces the amount of boilerplate code.
- Safe: Avoids entire classes of errors such as null pointer exceptions.
- Interoperable: Fully leverages existing libraries on the JVM, Android, and browsers.
- Tool-friendly: Can be built with any Java IDE or using the command line.