自从Kotlin 成为 Android 开发一级语言,Kotlin确实以其实用,高效赢得了海外很多公司和开发者的认可,比如Square的Jake大神一直在推Kotlin。Kotlin在国外至少有将近2年的应用生产环境的实践(非JetBrains内部实践应用)。在移动开发中,相比iOS程序员,Android程序员总是很幸运,因为我们有很多优秀好用的工具(Android Studio等),选用Kotlin,则是Google 为开发者提供高效的开发工具的一贯作风。
先来晒一晒Kotlin的几大特点:
Kotlin是静态类型编程语言,用于现代多平台应用,100%可与Java™和Android™互操作 [java] view plain copyKotlin是非常简介的编程语言
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"}[java] view plain copy
Kotlin 很安全
Get rid of those pesky NullPointerExceptions, you know, The Billion Dollar Mistake
var output: String
output = null // Compilation errorKotlin protects you from mistakenly operating on nullable typesval name: String? = null // Nullable type
println(name.length()) // Compilation errorAnd if you check a type is right, the compiler will auto-cast it for youfun calculateTotal(obj: Any) {
if (obj is Invoice)obj.calculateTotal()}[java] view plain copy
方便使用 兼容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.SchedulersFlowable
.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 toimport kotlin.browser.window
fun onLoad() {
window.document.body!!.innerHTML += "Hello, Kotlin!"}