https://kotlinlang.org logo
#getting-started
Title
# getting-started
y

y

03/01/2024, 9:05 PM
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

ephemient

03/01/2024, 9:07 PM
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

y

03/01/2024, 9:12 PM
so a unicode scalar value?
e

ephemient

03/01/2024, 9:12 PM
no
y

y

03/01/2024, 9:13 PM
oh sorry, I see the definition of code unit now.
and thank you for the information
e

ephemient

03/01/2024, 9:31 PM
(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
4 Views