How do primitives work in kotlin? ```public class...
# getting-started
u
How do primitives work in kotlin?
Copy code
public class Char private constructor() : Comparable<Char> {
    public fun toInt(): Int
    ...
}
First of all, what is this even, and why does it compile? a concrete class with no implementation of methods? Secondly, does
charArratOf('a', 'b', 'c')
create 3 instances of Char class? Or, is there some magic
e
that builtin "implementation" is just a stub. on JVM the compiler emits
java.lang.Character
,
char
,
char[]
, etc. as appropriate.
u
okay so
charArrayOf(..)
is then equal to the java
char[]
?
e
yes,
primitiveArrayOf()/PrimitiveArray
is exactly equivalent to Java's
primitive[]
, and
arrayOf<T>()/Array<T>
is exactly equivalent to Java's
T[]
u
thanks Id like how ask how does the Char class compile like that? Is there some special case in the compiler?
e
yes, builtins are hard-wired in the compiler
u
thank you
c
It doesn't actually compile to anything, what you're looking at is more or less a “binding” that declares functions, similar to external interfaces, so the rest of your code can call these methods; but the real implementation is inside the compiler/runtime.