generally, should I avoid using Java functions whe...
# getting-started
y
generally, should I avoid using Java functions when possible? (and if so, why?) should I go out of my way to use a Kotlin function, when a Java equivalent would be more convenient?
s
There are two reasons I can think of to prefer a Kotlin function over a Java equivalent: 1. Java functions are only available when targeting the JVM, whereas Kotlin standard library functions should be available for all platforms that Kotlin can run on. Obviously this is only important if you want to run your code on other platforms. 2. Kotlin functions may have more precise type signatures, because they always include nullability information. Java functions sometimes have this info added via annotations, but not always.
🧠 1
y
could there be (significant) performance gains from using a Kotlin function in some cases?
s
I would guess not. In many cases that I’ve seen, multiplatform functions are implemented using `expect`/`actual`, so that the Kotlin function actually calls through to the Java one when you run it on the JVM. Even if a function really is implemented twice, in pure Kotlin and pure Java, I don’t think there’s any reason to expect the performance to be significantly better in one or the other. That would come down to choices made in the specific implementation, not to the choice of language.
Do you have any specific functions that you’re wondering about?
y
I took the time now to learn about
expect
and
actual
. this is really just about me finding this interesting from an implementation perspective, and from a "how Kotlin works" perspective. so there's no specific example I can give. anyway - thanks, appreciate the help as always