Kotlin Tutorial
Kotlin is a statically typed programming language that runs on the Java Virtual Machine, known as the Swift of the Android world, designed and developed by JetBrains and open-sourced.
Kotlin can be compiled into Java bytecode, and it can also be compiled into JavaScript, facilitating operation on devices without a JVM.
At Google I/O 2017, Google announced Kotlin as the official Android development language.
My First Kotlin Program
Kotlin program files end with .kt
, such as: hello.kt, app.kt.
Minimalist Version
package hello // Optional package header
fun main(args: Array<String>) { // Package-visible function, accepts an array of strings as an argument
println("Hello World!") // Semicolons can be omitted
}
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
}
Why Choose Kotlin?
Concise: Greatly reduces the amount of boilerplate code.
Safe: Prevents entire classes of errors such as null pointer exceptions.
Interoperability: Fully leverage existing libraries of the JVM, Android, and browsers.
Tool-friendly: Can be used with any Java IDE or built using the command line.