table of Contents
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 or JavaScript, which is convenient to run on devices without JVM.
In Google I/O 2017, Google announced Kotlin as the official development language for Android.
- Kotlin tutorial Kotlin IntelliJ IDEA environment construction
- Kotlin Eclipse environment setup
- Kotlin uses the command line to compile
- Kotlin Android environment construction
- Kotlin basic syntax
- Kotlin basic data types
- Kotlin condition control
- Kotlin loop control
- Kotlin classes and objects
- Kotlin inheritance
- Kotlin interface
- Kotlin extension
- Kotlin data class and sealed class
- Kotlin generics
- Kotlin Enumeration
- Kotlin object expression/declaration
- kotlin commission
My first Kotlin program
Kotlin program files end with .kt, such as hello.kt and app.kt.
Minimal version
package hello // optional package header fun main(args: Array<String>) {// package-level visible function, accepts an array of strings as a parameter println("Hello World!") // semicolon can be omitted}
package hello // 可选的包头
fun main(args: Array<String>) { // 包级可见的函数,接受一个字符串数组作为参数
println("Hello World!") // 分号可以省略
}
Object-oriented
class Greeter(val name: String) {fun greet() {println("Hello, $name")}} fun main(args: Array<String>) {Greeter("World!").greet() // Create An object does not need the new keyword}
class Greeter(val name: String) {
fun greet() {
println("Hello, $name")
}
}
fun main(args: Array<String>) {
Greeter("World!").greet() // 创建一个对象不用 new 关键字
}
Why choose Kotlin?
- Simplicity: Greatly reduce the amount of boilerplate code.
- Security: Avoid the entire class of errors such as null pointer exceptions.
- Interoperability: Make full use of the existing libraries of JVM, Android and browsers.
- Tool-friendly: It can be built using any Java IDE or using the command line.