Equivalent of Java String[] in Kotlin

  1. the Reason Behind No StringArray[] in Kotlin
  2. Java String[] Equivalent to Create Kotlin String Array

Kotlin has arrays equivalent to Java’s primitive data type arrays. For instance, it has:

  • ByteArray = byte[]
  • ShortArray = short[]
  • IntArray = int[]
  • CharArray = char[]
  • DoubleArray = double[]
  • FloatArray = float[]

However, Kotlin does not have the same for Java’s String[]. So there’s nothing like StringArray[] in Kotlin.

But what if we want to create an empty array of string data types? How would we do that in Kotlin?

We will learn the answers to these questions in this article. But before heading to that, let’s understand why there’s no StringArray[] in Kotlin.

the Reason Behind No StringArray[] in Kotlin

There’s no StringArray[] in Kotlin because the string is not a primitive data type for JVM. In contrast, other types with array methods like ByteArray[], ShortArray, IntArray[], etc., are primitive.

The primitive types are stored after boxing them as objects. The specialized Kotlin arrays, such as IntArray[] and others, unbox these primitive data types and keep them.

However, since a string is not a primitive type, it does not require a specialized array. Hence, there is no StringArray[] in Kotlin.

Java String[] Equivalent to Create Kotlin String Array

We can still create string arrays in Kotlin using different ways. All these ways can be said to be Java String[] equivalent in Kotlin.

Here are the different ways to create an empty string array in Kotlin.

Use arrayOf<String> to Create an Empty Array in Kotlin

fun main(args: Array<String>) {

    var myStringArray = arrayOf<String>()

}

Use arrayOf to Create an Empty Array in Kotlin

fun main(args: Array<String>) {

    var myStringArray = arrayOf("", "", "")

}

Use Array<String?> to Create an Empty Array in Kotlin

fun main(args: Array<String>) {

    var myStringArray = Array<String?>(5) { null }

}

Use Array<String> to Create an Empty Array in Kotlin

fun main(args: Array<String>) {

    var myStringArray = Array<String>(5) { "it = $it" }

}

Use arrayOfNulls<String> to Create an Empty Array in Kotlin

fun main(args: Array<String>) {

    var myStringArray = arrayOfNulls<String>(5)

}

Besides the empty arrays, we can use the same syntax to create initialized arrays, too. The only method that won’t work while creating an initialized array is arrayOfNulls<String>.

Use arrayOf<String> to Create an Empty Array in Kotlin

Here’s an example of creating an initialized array using the arrayOf<String>.

fun main(args: Array<String>) {

    var myStringArray = arrayOf<String>("Hello", "Welcome", "to", "Kotlin", "Tutorials")

    myStringArray.forEach {
        println(it)
    }

}

Output:

Create Initialized Array

Similarly, we can use arrayOf, Array<String?>, and Array<String> to create initialized Kotlin string arrays.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Kotlin Array

  • Initialize Array in Kotlin With Values