Kotlin简介

语言时代

这是一个“快”的时代:快速原型,快速迭代;“快”的开发需求催生了一大批语言: Clojure,Elixir, Go,Groovy,PHP, Python, Ruby, Scala。

这些语言有些是浴火重生,有些是顺势而生,有些则自学术领域;在这个大时代里,只经过短短几年发展,就在各适用领域取得很大成绩,比如:Docker,Gradle,Node,Play!……

这波服务器端开发工具的变革,也引发了客户端开发工具的震动:Github开源了Electron,Swift被引入iOS。

在各开发领域里,除了web,最火的就是Android。Java是Android的官方语言,但语法的设计思想还停留在纯命令式语言时代,在风起云涌的函数式语言大背景下,显得有点落伍,虽然有了Java8,但现阶段还惠及不到Android开发者。

落后的编程语言阻碍了生产力!

所以,JetBrains为Android带来了Kotlin!

JetBrains简介

JetBrains是一家伟大的科技公司,经过10多年的努力,在IDE领域有了丰厚的积累,创建了一系列跨平台开发环境: IntelliJ IDEA, Clion,PyCharm…这些IDE覆盖了主流编程语言(C/C++, Java, Php, Web, Python, Ruby, Scala)和开发框架(Play!, RoR, Django)。

这些JetBrains系IDE共有的特性包括:

  • 强大的代码提示,协助开发者编写更高效、更Idiom的代码
  • 集成现代Build工具(Gradle, SBT)

此外,特定语言的IDE支持更领域化的功能(Debugger, Monitor),让开发过程有了质变。
Android Studio's Monitor

JetBrains系列产品凭借这些强大功能,吸引了一大批用户,曾经作为Java标准IDE的Eclipse风光不再。

取得这样的成绩肯定要付出巨大心血,感谢JetBrains。
JetBrains Tools

Eclipse的故事

决定编程语言能否成功有三个要素:

  • 编程语言本身
  • 标准库
  • IDE

Sun创造了Java和标准库(J2EE, J2SE, J2ME),但多年来都没有为Java提供一个好用的IDE;在这样的局面下,IBM适时推出Eclipse,作为对抗Sun的武器;十几年来,Eclipse凭借开源、可扩展平台(OSGi)和插件(JDT),战胜了一个个对手(JBuilder, NetBeans),奠定了Java标准开发环境的地位。

像所有成功的产品一样,Eclipse一路荣光走来的同时背上太多历史包袱,船大调头难,在JetBrains一系列产品的冲击下,渐显疲态。

Eclipse Screenshot

Kotlin概述

在这个语言时代,Jvm作为一个优秀的VM环境,其上诞生了很多新语言,他们声称要“变革Java”;Kotlin就是这批语言中的一个,但与其他语言相比,属于温和的“改良派”,其语法和Java很像(容易上手),并推荐以循序渐进的方式开发项目:

  • 允许项目中同时存在Java和Kotlin代码文件
  • 允许Java与Kotlin互调

这使得开发者可以很方便的在已有项目中引入Kotlin;新模块用Kotlin,稳定模块勿需用Kotlin重写。

JetBrains就是这样做的:

For example, IntelliJ IDEA is written in Java and we are willing to use Kotlin
in this project. Of course, we are not going to rewrite the whole 10-year-old
code base in another language. Most likely, we will start by writing tests in
Kotlin, then new features, then maybe some existing subsystems that will be
migrated to Kotlin while under refactoring.

Kotlin语法

本文把Kotlin语法功能分为两类:

  • 创建更紧凑的代码
  • 减少错误

创建更紧凑的代码

Data Class(POJO)

Java Code

class Book {
        private String title;
        private Author author;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public Author getAuthor() {
            return author;
        }

        public void setAuthor(Author author) {
            this.author = author;
        }
    }

Kotlin Code

data class User(val name: String, val age: Int)

Type inference

val arrayList = arrayListOf("Kotlin", "Scala", "Groovy") 

不必显示写出arrayList的类型


String Templates

fun main(args: Array<String>) {
    if (args.size == 0) return

    print("First argument: ${args[0]}")
}

有了模板功能,再也不用费力的拼字符串了,让你有能力创建更有意义的Log。


let, apply
善用这两个函数,可以免去很多为变量起名的烦恼。
exploring kotlin std lib这篇文章对这两个函数做了很好的说明,本文不再累述。


it
如果字面函数只有一个参数,可以省略该参数声明,并用“it”代替。

val doubled = ints.map { it -> it * 2 }

可以替换为:

val doubled = ints.map { it * 2 }

SAM conversions
字面函数转换为接口实现。

val runnable = Runnable { println("This runs in a runnable") }

严格定义(kotlin-docs.pdf, p117):

Just like Java 8, Kotlin supports SAM conversions. This means that Kotlin
function literals can be automatically converted into implementations of Java
interfaces with a single non-default method, as long as the parameter types of the
interface method match the parameter types of the Kotlin function.

减少错误

Null Safety
Java中最痛苦莫过于要写大量代码测试null,即使是这样,你也不敢担保彻底消灭NPE(一个疏忽,或第三方库)。

感谢Kotlin,为我们带来编译期null检查,让我们有信心写出无NPE的代码。

var a: String = "abc"
a = null // compilation error

var b: String? = "abc"
b = null // ok

val l = b.length // compilation error: variable 'b' can be null

官方文章对“Null safety”做了详细说明,本文不再累述。


Named Arguments
避免我们把参数顺序搞错

fun update(url: String, status: String) {
    ...
}

update(url="http://addr", status = "stop")

参考

Kotlin Official Site
https://kotlinlang.org/

Kotlin 1.0 Released: Pragmatic Language for JVM and Android
https://blog.jetbrains.com/kotlin/2016/02/kotlin-1-0-released-pragmatic-language-for-jvm-and-android/

JetBrains:
https://www.jetbrains.com/
https://en.wikipedia.org/wiki/JetBrains

JetBrains Tools:
https://www.jetbrains.com/products.html

10 Features You Will Love About Kotlin
https://github.com/KotlinBy/awesome-kotlin/blob/master/app/rss/articles/Kotlin%20for%20Java%20Developers:%2010%20Features%20You%20Will%20Love%20About%20Kotlin.md

The Advent of Kotlin: A Conversation with JetBrains’ Andrey Breslav
http://www.oracle.com/technetwork/articles/java/breslav-1932170.html

Kotlin Vs. Scala
https://kotlinlang.org/docs/reference/comparison-to-scala.html

Null Safety
https://kotlinlang.org/docs/reference/null-safety.html

Exploring the Kotlin standard library
http://beust.com/weblog/2015/10/30/exploring-the-kotlin-standard-library/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值