Kotlin defines its own String
class:
public class String : Comparable<String>, CharSequence {
companion object {}
public operator fun plus(other: Any?): String
public override val length: Int
public override fun get(index: Int): Char
public override fun subSequence(startIndex: Int, endIndex: Int): CharSequence
public override fun compareTo(other: String): Int
}
The instances of this class are constructed via inline functions defined in StringsJVM.kt
:
public inline fun String(bytes: ByteArray, offset: Int, length: Int): String =
java.lang.String(bytes, offset, length, Charsets.UTF_8) as String
Questions:
- How does the cast of
java.lang.String
tokotlin.String
work? It is not a subtype but a separate implementation ofCharSequence
- Member
length
is a val with no value assigned. How does this compile? It would need to be a lateinit var
I presume there must some sort of code pre-processing going on before the compilation.
Please feel free to edit this question with a more apt title.