欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Why Kotlin?

程序员文章站 2022-04-26 15:47:46
...


Kotlin是什么:

静态类型编程语言

用于现代多平台应用

100%可与Java™和Android™互操作

#Kotlin是非常简介的编程语言

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"
}


#Kotlin 很安全

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()
}

#方便使用 兼容JVM上现有library

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!"
}

很好的工具

Why Kotlin?

Why Kotlin?


Why Kotlin?




您接触过Kotlin吗?

您是否考虑尽快转换Kotlin开发呢?

Kotlin使用中遇到什么问题吗?

对于Kotlin的发展您怎么看?