Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 541
Philipp Claßen Avatar asked Dec 11 '15 17:12

Philipp Claßen


2 Answers

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

like image 93
2 revs, 2 users 67% Avatar answered Nov 15 '22 21:11

2 revs, 2 users 67%


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

like image 42
user2235698 Avatar answered Nov 15 '22 21:11

user2235698