On the Rise of Kotlin

On the Rise of Kotlin

JUNE 20, 2017On the Rise of Kotlin

https://blog.heroku.com/rise-of-kotlin

JUNE 20, 2017

POSTED BY JOE KUTNER

It’s rare when a highly structured language with fairly strict syntax sparks emotions of joy and delight. But Kotlin, which is statically typed and compiled like other less friendly languages, delivers a developer experience that thousands of mobile and web programmers are falling in love with.
The designers of Kotlin, who have years of experience with developer tooling (IntelliJ and other IDEs), created a language with very specific developer-oriented requirements. They wanted a modern syntax, fast compile times, and advanced concurrency constructs while taking advantage of the robust performance and reliability of the JVM. The result, Kotlin 1.0, was released in February 2016 and its trajectory since then has been remarkable. Google recently announced official support for Kotlin on Android, and many server-side technologies have introduced Kotlin as a feature.

The Spring community announced support for Kotlin in Spring Framework 5.0 last month and the Vert.x web server has worked with Kotlin for over a year. Kotlin integrates with most existing web applications and frameworks out-of-the-box because it's fully interoperable with Java, making it easy to use your favorite libraries and tools.
But ultimately, Kotlin is winning developers over because it’s a great language. Let’s take a look at why it makes us so happy.

A Quick Look at Kotlin

The first thing you’ll notice about Kotlin is how streamlined it is compared to Java. Its syntax borrows from languages like Groovy and Scala, which reduce boilerplate by making semicolons optional as statement terminators, simplifying for loops, and adding support for string templating among other things. A simple example in Kotlin is adding two numbers inside of a string like this:

val sum: String = "sum of $a and $b is ${a + b}"

The val keyword is a feature borrowed from Scala. It defines an read-only variable, which in this case is explicitly typed as a String
. But Kotlin can also infer that type. For example, you could write:

val x = 5

In this case, the type Int is inferred by the compiler. That’s not to say the type is dynamic though. Kotlin is statically typed, but it uses type inference to reduce boilerplate.

Like many of the JVM languages it borrows from, Kotlin makes it easier to use functions and lambdas. For example, you can filter a list by passing it an anonymous function as a predicate:

val positives = list.filter { it > 0 }

The it variable in the function body references the first argument to the function by convention. This is borrowed from Groovy, and eliminates the boilerplate of defining parameters.

You can also define named functions with the fun keyword. The following example creates a function with default arguments, another great Kotlin feature that cleans up your code:

fun printName(name: String = "John Doe") { println(name);}

But Kotlin does more than borrow from other languages. It introduces new capabilities that other JVM languages lack. Most notable are null safety and coroutines.

Null safety means that a Kotlin variable cannot be set to null
unless it is explicitly defined as a nullable variable. For example, the following code would generate a compiler error:

val message: String = null

But if you add a ? to the type, it becomes nullable. Thus, the following code is valid to the compiler:

val message: String? = null

Null safety is a small but powerful feature that prevents numerous runtime errors in your applications.

Coroutines, on the other hand, are more than just syntactic sugar. Coroutines are chunks of code that can be suspended to prevent blocking a thread of execution, which greatly simplifies asynchronous programming.
For example, the following program starts 100,000 coroutines using the launch function. The body of the coroutine can be paused at a suspension point so the main thread of execution can perform some other work while it waits:

fun main(args: Array<String>) = runBlocking<Unit> {
  var number = 0
  val random = Random()
  val jobs = List(100_000) {
    launch(CommonPool) {
      delay(10)
      number += random.nextInt(100)
    }
  }
  jobs.forEach { it.join() }
  println("The answer is: $number")
}

The suspension point is the delay call. Otherwise, the function simply calculates some random number and renders it.

Coroutines are still an experimental feature in Kotlin 1.1, but early adopters can use them in their applications today.

Despite all of these great examples, the most important feature of Kotlin is its ability to integrate seamlessly with Java. You can mix Kotlin code into an application that’s already based on Java, and you can consume Java APIs from Kotlin with ease, which smooths the transition and provides a solid foundation.

Kotlin Sits on the Shoulders of Giants

Behind every successful technology is a strong ecosystem. Without the right tools and community, a new programming language will never achieve the uptake required to become a success. That’s why it’s so important that Kotlin is built into the Java ecosystem rather than outside of it.

Kotlin works seamlessly with Maven and Gradle, which are two of the most reliable and mature build tools in the industry. Unlike other programming languages that attempted to separate from the JVM ecosystem by reinventing dependency management, Kotlin is leveraging the virtues of Java for it's tooling. There are attempts to create Kotlin-based build tools, which would be a great addition to the Kotlin ecosystem, but they aren't a prerequisite for being productive with the language.

Kotlin also works seamlessly with popular JVM web frameworks like Spring and Vert.x. You can even create a new Kotlin-based Spring Boot application from the Spring Initializer web app. There has been a huge increase in adoption of Kotlin for apps generated this way.

Kotlin has great IDE support too, thanks to it's creators. The best way to learn Kotlin is by pasting some Java code into IntelliJ and allowing the IDE to convert it to Kotlin code for you. All of these pieces come together to make a recipe for success. Kotlin is poised to attract both new and old Java developers because it's built on solid ground.

If you want to see how well Kotlin fits into existing Java tooling, try deploying a sample Kotlin application on Heroku using our Getting Started with Kotlin guide. If you're familiar with Heroku, you'll notice that it looks a lot like deploying any other Java-based application on our platform, which helps make the learning curve for Kotlin relatively flat. But why should you learn Kotlin?

Why Kotlin?

Heroku already supports five JVM languages that cover nearly every programming language paradigm in existence. Do we need another JVM Language? Yes. We need Kotlin as an alternative to Java just as we needed Java as an alternative to C twenty years ago. Our existing JVM languages are great, but none of them have demonstrated the potential to become the de facto language of choice for a large percentage of JVM developers.
Kotlin has learned from the JVM languages that preceded it and borrowed the best parts from those ecosystems. The result is a well round, powerful, and production-ready platform for your apps.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末池凄,一起剝皮案震驚了整個(gè)濱河市鬼廓,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌尤慰,老刑警劉巖雷蹂,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件匪煌,死亡現(xiàn)場離奇詭異党巾,居然都是意外死亡齿拂,警方通過查閱死者的電腦和手機(jī)肴敛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來砸狞,“玉大人镀梭,你說我怎么就攤上這事丰辣。” “怎么了笙什?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長芽隆。 經(jīng)常有香客問我统屈,道長,這世上最難降的妖魔是什么腕扶? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮吨掌,結(jié)果婚禮上半抱,老公的妹妹穿的比我還像新娘。我一直安慰自己膜宋,他們只是感情好窿侈,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著秋茫,像睡著了一般史简。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上肛著,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天圆兵,我揣著相機(jī)與錄音,去河邊找鬼衙傀。 笑死抬吟,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的统抬。 我是一名探鬼主播火本,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼聪建!你這毒婦竟也來了钙畔?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤金麸,失蹤者是張志新(化名)和其女友劉穎擎析,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體挥下,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡揍魂,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了棚瘟。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片现斋。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖偎蘸,靈堂內(nèi)的尸體忽然破棺而出庄蹋,到底是詐尸還是另有隱情,我是刑警寧澤迷雪,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布限书,位于F島的核電站,受9級(jí)特大地震影響章咧,放射性物質(zhì)發(fā)生泄漏倦西。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一赁严、第九天 我趴在偏房一處隱蔽的房頂上張望扰柠。 院中可真熱鬧,春花似錦误澳、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至踱承,卻和暖如春倡缠,著一層夾襖步出監(jiān)牢的瞬間哨免,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國打工昙沦, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留琢唾,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓盾饮,卻偏偏與公主長得像采桃,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子丘损,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容