Let's say that we have the following:
val person = "Bill"
Could someone explain the difference between these two:
val kClass1 = person.javaClass.kotlin
vs
val kClass2 = person::class
When I should call the one instead of the other?
Any source code example would be appreciated.
The main reason there are two ways to achieve the same thing, namely get the Kotlin class of an object, is because prior to Kotlin 1.1, the ::class
literal did not support the expression on its left-hand side. So if you're using Kotlin 1.0, your only option is .javaClass.kotlin
, otherwise you're fine with either of them. This is the reason "Kotlin in Action" uses the .javaClass.kotlin
syntax: it was written before the Kotlin 1.1 release.
There's also a minor difference in the types of these expressions. For example, in the following code
interface T
fun f1(x: T) = x::class
fun f2(x: T) = x.javaClass.kotlin
f1
's type is KClass<out T>
, but f2
's type is KClass<T>
. This is actually an oversight in the javaClass
declaration: KClass<out T>
is more correct in this case, because x
's class is not necessarily T
, but can also be a subclass of T
.
Otherwise these two expressions (x.javaClass.kotlin
and x::class
) are completely equivalent in terms of produced bytecode and runtime performance. I prefer x::class
because it's shorter and reads better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With