I cannot understand the implementation of the source code string.kt in kotlin
In kotlin source code I can't figure out how to implement the length of String.kt, it looks like this:
package kotlin
public class String : Comparable<String>, CharSequence {
companion object {}
/**
* Returns a string obtained by concatenating this string with the string representation of the given [other] 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}
var len:Int = "abc".length; // len = 3 where to run the length??
where to implement the length function?
+3
source to share
1 answer
String functions are examples of what Kotlin considers Intrinsic
functions. They are defined based on the platform they run on and you cannot find their implementation in the source code.
For the JVM, they will be directly mapped to the corresponding native methods java.lang.String
. This ensures that there is no runtime overhead and uses optimizations done in the java standard library.
+9
source to share