Philipp Claßen
Philipp Claßen

Reputation: 44009

Kotlin: What features of Java 8 are not yet supported?

Is there an up-to-date overview of Java 8 features, which are not yet supported in Kotlin?


For example, calling a default method like Map#putIfAbsent fails to compile (unsupported reference error):

import java.util.*

fun main(args: Array<String>) {
    val x : Map<Int, Int> = HashMap<Int, Int>()
    x.putIfAbsent(1, 2)
}

If the default method is overridden, it works:

import java.util.*

fun main(args: Array<String>) {
    val x : HashMap<Int, Int> = HashMap<Int, Int>()
    x.putIfAbsent(1, 2)
}

That is what I found out by experiments, but for deciding whether migrating from a Java 8 code basis to Kotlin is already possible, a systematic overview would be valuable.


Update: The code in my example was created by the "Java to Kotlin" converter. As user2235698 pointed out, Map<Int, Int> is a immutable Kotlin map. Still, the example fails to compile when I change it to a java.util.Map map. My claim that it has to do something with default methods, however, is misleading.

As it is beyond the scope of this question, I opened a follow-up question, here: Does java.util.HashMap not implement java.util.Map in Kotlin?

Upvotes: 9

Views: 756

Answers (2)

user2235698
user2235698

Reputation: 7619

Map is immutable and HashMap is mutable in Kotlin, that's why you can't put key-value pair in the first case.

More details

Upvotes: 3

Philipp Cla&#223;en
Philipp Cla&#223;en

Reputation: 44009

Known Java 8 interoperability issues are tracked as subtasks of this issue

Upvotes: 9

Related Questions