I see that Kotlin has ByteArray, ShortArray, IntArray, CharArray, DoubleArray, FloatArray
, which are equivalent to byte[], short[], int[],char[], double[], float[]
in Java.
Now I'm wondering, is there any StringArray
equivalent to Java's String[]
?
Best Answer
There's no special case for
String
, becauseString
is an ordinary referential type on JVM, in contrast with Java primitives (int
,double
, ...) -- storing them in a referenceArray<T>
requires boxing them into objects likeInteger
andDouble
. The purpose of specialized arrays likeIntArray
in Kotlin is to store non-boxed primitives, getting rid of boxing and unboxing overhead (the same as Javaint[]
instead ofInteger[]
).You can use
Array<String>
(andArray<String?>
for nullables), which is equivalent toString[]
in Java:See also: Arrays in the language reference