Introduction: Why This Question Haunts Everyone Moving from Java to Kotlin
Have you just started learning Kotlin after Java and already encountered your first surprise? You try to declare a string array the way you're used to in Java — String[] names = {"Alice", "Bob"} — and the Kotlin compiler throws an error? Congratulations, you are not alone. The question "What's the Kotlin equivalent of Java's String[]?" has gathered thousands of views on Stack Overflow, and for good reason.
Kotlin is not just "Java with sugar." Google positions it as a more modern, safer, and more concise language. But this conciseness can sometimes confuse beginners. In Java, an array is a built-in language construct. In Kotlin, arrays are part of the standard library, and they have their own quirks.
In this article, we'll cover everything you need to know about Kotlin string arrays. You'll learn not only the direct equivalent of String[], but also more flexible alternatives that will make your code cleaner and safer. Let's go!
1. The Direct Equivalent: Array<String>
The simplest and most obvious way to replace String[] in Kotlin is to use Array<String>. In Kotlin, all arrays are objects of the class Array<T>, where T is the element type.
How to Declare and Initialize?
In Java you write:
String[] names = new String[3];names[0] = "Alice";names[1] = "Bob";names[2] = "Charlie";In Kotlin it looks like this:
val names: Array<String> = arrayOfNulls(3)names[0] = "Alice"names[1] = "Bob"names[2] = "Charlie"But there is a much shorter way — use the arrayOf() function:
val names = arrayOf("Alice", "Bob", "Charlie")The Kotlin compiler will infer the type Array<String>. Note: unlike Java, in Kotlin there is no new operator for creating arrays. Instead, factory functions are used.
Important Difference: Fixed Size
As in Java, the size of Array<String> is fixed. You cannot add a new element after creation. If you try to access an index beyond the length, you'll get an ArrayIndexOutOfBoundsException.
val fruits = arrayOf("Apple", "Banana")// fruits[2] = "Cherry" // Error! Index out of bounds2. Specialized Arrays: Not Always Array<String>
For primitive types (Int, Double, Boolean), Kotlin offers optimized classes: IntArray, DoubleArray, BooleanArray, etc. They don't use autoboxing/unboxing, which saves memory and speeds up execution.
But there is no such optimization for strings, because String is a reference type, not a primitive. Therefore, Array<String> is the only option if you specifically need an array.
3. Alternative #1: List<String> (Immutable List)
In most cases, you don't need an array. In Kotlin, it is strongly recommended to use lists instead of arrays, unless you are working with low-level code (e.g., JNI or nanosecond-level performance optimization).
A List<String> is an immutable collection. You cannot change its size or elements after creation. This makes the code safer in a multithreaded environment.
val namesList: List<String> = listOf("Alice", "Bob", "Charlie")// namesList[0] = "Eve" // Error! listOf returns an immutable listEssentially, listOf is the Kotlin equivalent of Java String[] with added protection. In Java, an array can be accidentally modified by passing it to another method. In Kotlin, List prevents this.