Programming language for mobile cross-platform
and web development, server-side, native
and data science. Open source forever Github
All the resources you need to learn Kotlin, in one place and easy to navigate
fun main() {
println("Hello World")
}
class Greeter(val name: String) {
fun greet() {
println("Hello, $name")
}
}
fun main(args: Array<String>) {
Greeter(args[0]).greet()
}
import kotlinx.coroutines.*
//sampleStart
suspend fun main() = coroutineScope {
for (i in 0 until 10) {
launch {
delay(1000L - i * 10)
print("❤️$i ")
}
}
}
//sampleEnd
Drastically reduce the amount of boilerplate code.
/*
Create a POJO with getters, setters, `equals()`, `hashCode()`, `toString()` and `copy()` in a 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"
}
Avoid entire classes of errors such as null pointer exceptions.
/*
Get rid of those pesky NullPointerExceptions, you know, The Billion Dollar Mistake
*/
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()
}
Leverage existing libraries for the JVM, Android, and the browser.
/*
Use any existing library on the JVM, as there’s 100% compatibility, including SAM support.
*/
import io.reactivex.Flowable
import io.reactivex.schedulers.Schedulers
Flowable
.fromCallable {
Thread.sleep(1000) // imitate expensive computation
"Done"
}
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.single())
.subscribe(::println, Throwable::printStackTrace)
// 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!"
}
Choose any Java IDE or build from the command line.
Bundled with Community Edition or IntelliJ IDEA Ultimate
Bundled with Studio 3.0, plugin available for earlier versions
Install the plugin from the Eclipse Marketplace
Use any editor and build from the command line