On the Rise of Kotlin

简介: On the Rise of KotlinJUNE 20, 2017On the Rise of Kotlinhttps://blog.heroku.

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.

相关文章
|
28天前
|
设计模式 安全 Java
Kotlin
Kotlin
17 0
|
编译器 开发者 Kotlin
Kotlin Contract
Kotlin Contract
154 0
|
安全 调度 数据库
[译] 关于 Kotlin Coroutines, 你可能会犯的 7 个错误
[译] 关于 Kotlin Coroutines, 你可能会犯的 7 个错误
[译] 关于 Kotlin Coroutines, 你可能会犯的 7 个错误
|
开发者 C++ Kotlin
Kotlin Coroutines Flow 系列(五) 其他的操作符
Kotlin Coroutines Flow 系列(五) 其他的操作符
171 0
|
安全 Java 调度
Kotlin Coroutines 笔记 (二)
Kotlin Coroutines 笔记 (二)
149 0
Kotlin Coroutines 笔记 (二)
|
编译器 API 调度
Kotlin Coroutines 笔记 (一)
Kotlin Coroutines 笔记 (一)
130 0
Kotlin Coroutines 笔记 (一)
分享Kotlin的一个小技巧
分享Kotlin的一个小技巧
|
SQL 前端开发 JavaScript
Kotlin的分享
在公司部门小组中做了一次kotlin的分享 # kotlin简介 Kotlin是由JetBrains公司(IDEA开发者)所开发的编程语言,其名称来自于开发团队附近的科特林岛。 多平台开发 JVM :Android; Server-Side Javascript:前端 Native(beta) :开发原生应用 windows、macos、linux Swift与Kotli
1503 0
Kotlin可控性探索
目录介绍 01.可空性 02.安全调用运算符:?. 03.Elvis运算符:?: 04.安全转换运算符:as? 05.非空断言:!! 06.let函数说明 07.可空类型的扩展 08.Java中判断方式 09.
1285 0
|
JavaScript Java 开发者
Overview of Kotlin & Comparison Between Kotlin and Java
Overview of Kotlin & Comparison Between Kotlin and Java by Navdeep Singh Gill What is Kotlin? Kotlin is a new programming language from JetBrains.
1305 0