使用一行代码创建一个包含getters, setters, equals()
, hashCode()
, toString()
and copy()
的POJO
data class Customer(val name: String, val email: String, val company: String)
或者使用lambda表达式来过滤链表:
val positiveNumbers = list.filter {it > 0}
想要单例?创建object就可以了:
object ThisIsASingleton {
val companyName: String = "JetBrains"
}
彻底告别那些烦人的 NullPointerExceptions, 毕竟价值万亿
var output : String
output = null
And of course, Kotlin protects you from mistakenly operating on nullable types, including those from Java Kotlin会预防你对可能为null的对象进行操作, 哪怕是Java中传递过来的
println(output.length())
如果你进行了类型检测, 那么编译器会在检测后的代码分支内自动帮你转换类型
fun calculateTotal(obj: Any) {
if (obj is Invoice) {
obj.calculateTotal()
}
}
多用途的语言可用于构建各式各样的程序
Android开发
不影响性能
超小的运行时依赖
服务器端程序
与JVM框架百分百兼容, 用什么服务器请随意
JavaScript. 用Kotlin写的代码可以生成JavaScript运行于Node.js或者浏览器中
企业版. 使用Kotlin进行任何类型的企业级应用程序Java EE开发.
Web. 无论是强类型的HTML与CSS开发亦或纯web开发
随意使用Java代码
import io.netty.channel.ChannelInboundMessageHandlerAdapter
import io.netty.channel.ChannelHandlerContext
public class NettyHandler: ChannelInboundMessageHandlerAdapter<Any>() {
public override fun messageReceived(p0: ChannelHandlerContext?, p1: Any?) {
throw UnsupportedOperationException()
}
}
或者在JVM上使用已有Java库, 百分百兼容, 包括SAM支持
无论是JVM还是JavaScript, 用Kotlin写代码, 然后部署到你想要的地方.
import js.dom.html.*
fun onLoad() {
window.document.body.innerHTML += "<br/>Hello, Kotlin!"
}
简洁,简单,并且非常容易阅读(以及写)
1.0.3
A stable milestone version of Kotlin can be obtained from JetBrains Plugin Repository.
In IDEA navigate to: Settings -> Plugins -> Browse Repositories
Select the Kotlin plugin and then Download and Install.
In IDEA navigate to: Preferences -> Plugins -> Browse Repositories. Enter:
Then you should be able to select the Kotlin plugin and Download and Install. The repository above contains nightly builds published automatically.