Kotlin如何能够使用JDK 1.8调用Java 11代码?

huangapple go评论132阅读模式
英文:

How is Kotlin able to call Java 11 code using JDK 1.8?

问题

我正在玩一个 Kotlin 项目,其中有一些 Java 11 的调用,但注意到它似乎可以使用 Java 8 来构建和运行。

特别的调用是 java.lang.String#isBlank(),这是在 Java 11 中引入的。

我正在使用 Gradle 来构建和运行测试。构建的 JDK 设置为 JDK 1.8,而 Kotlin 的编译目标也是 1.8:

  1. tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
  2. kotlinOptions {
  3. freeCompilerArgs += listOf("-Xjsr305=strict")
  4. jvmTarget = "1.8"
  5. }
  6. }
  1. gradle -v
  2. ------------------------------------------------------------
  3. Gradle 6.6
  4. ------------------------------------------------------------
  5. Build time: 2020-08-10 22:06:19 UTC
  6. Revision: d119144684a0c301aea027b79857815659e431b9
  7. Kotlin: 1.3.72
  8. Groovy: 2.5.12
  9. Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020
  10. JVM: 1.8.0_265 (AdoptOpenJDK 25.265-b01)
  11. OS: Mac OS X 10.15.6 x86_64

这是一个单元测试,调用了 Java 11,但仍然能够编译和运行:

  1. import org.junit.jupiter.api.Test
  2. class BlankTest {
  3. @Test
  4. fun testBlank() {
  5. val s = java.lang.String("test string")
  6. val blank = s.isBlank()
  7. println("Input string blank? : $blank")
  8. }
  9. }

这是如何可能的呢?

英文:

I was playing around with a Kotlin project which has some Java 11 calls and noticed that it seems to build and run fine using Java 8.

The call in particular is to java.lang.String#isBlank(), which was introduced in Java 11.

I'm using Gradle to build and run tests. Build JDK is set to JDK 1.8 and Kotlin compile target is also 1.8:

  1. tasks.withType&lt;org.jetbrains.kotlin.gradle.tasks.KotlinCompile&gt; {
  2. kotlinOptions {
  3. freeCompilerArgs += listOf(&quot;-Xjsr305=strict&quot;)
  4. jvmTarget = &quot;1.8&quot;
  5. }
  6. }
  1. gradle -v
  2. ------------------------------------------------------------
  3. Gradle 6.6
  4. ------------------------------------------------------------
  5. Build time: 2020-08-10 22:06:19 UTC
  6. Revision: d119144684a0c301aea027b79857815659e431b9
  7. Kotlin: 1.3.72
  8. Groovy: 2.5.12
  9. Ant: Apache Ant(TM) version 1.10.8 compiled on May 10 2020
  10. JVM: 1.8.0_265 (AdoptOpenJDK 25.265-b01)
  11. OS: Mac OS X 10.15.6 x86_64

This is a unit test which calls Java 11 but still compiles and runs:

  1. import org.junit.jupiter.api.Test
  2. class BlankTest {
  3. @Test
  4. fun testBlank() {
  5. val s = java.lang.String(&quot;test string&quot;)
  6. val blank = s.isBlank()
  7. println(&quot;Input string blank? : $blank&quot;)
  8. }
  9. }

How is this possible?

答案1

得分: 6

答案是,实际上你并没有使用 Java 11 的 isBlank() 方法,你在调用的是 Kotlin 的 isBlank 扩展函数,该函数可以应用于 java.lang.String。

英文:

The answer is that you're not actually using Java 11's isBlank() method, you're calling Kotlin's isBlank extension function that can be applied to java.lang.String.

huangapple
  • 本文由 发表于 2020年8月17日 22:52:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/63453344.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定