how are `String`s represented in Kotlin, internall...
# getting-started
y
how are `String`s represented in Kotlin, internally? as a sequence of bytes? (and if so, why can you not get a bytes array from a String in
O(1)
time?) or is
String
a compiler builtin?
e
depends on the platform, but conceptually a
String
is a wrapper around an immutable
CharArray
(on JVM, it's actually a
java.lang.String
at runtime, which has an internal
byte[]
representation, plus a bit to configure whether it's ISO-8859-1 or UTF-16 encoded)
to maintain immutability, to get a
ByteArray
it needs to make a copy either way
btw, a
Char
is a UTF-16 code unit. it is neither a byte nor a Unicode character
😢 2
y
so a unicode scalar value?
e
no
y
oh sorry, I see the definition of code unit now.
and thank you for the information
e
(possibly excessively pedantic) also,
String
doesn't validate that UTF-16 surrogates are properly paired either. in case it's not legal Unicode, a string may not be encodable to UTF-8 bytes