Question
In Kotlin, there are specialized array types such as ByteArray, ShortArray, IntArray, CharArray, DoubleArray, and FloatArray, which correspond to Java primitive arrays like byte[], short[], int[], char[], double[], and float[].
What is the Kotlin equivalent of Java's String[]? Is there a StringArray type, or should strings be represented differently in Kotlin?
Short Answer
By the end of this page, you will understand why Kotlin has special array types for primitives but not for String, and why the Kotlin equivalent of Java's String[] is Array<String>. You will also learn how to create, access, modify, and use string arrays in real Kotlin code.
Concept
Kotlin uses specialized array types like IntArray and DoubleArray only for primitive values. These exist mainly for performance and Java interoperability.
String is not a primitive type in Kotlin or Java. It is an object type. Because of that, Kotlin does not provide a special StringArray type.
The Kotlin equivalent of Java's:
String[] names;
is:
Array<String> names
Why this design exists
In Java, primitive arrays like int[] store raw primitive values efficiently. Object arrays like String[] store references to objects.
Kotlin reflects that distinction:
- Primitive arrays use specialized types:
IntArrayDoubleArrayBooleanArray- and others
- Object arrays use the generic
Array<T>type:
Mental Model
Think of Kotlin arrays as two storage shelves:
- One shelf is specially built for primitive values like
IntandDoubleto save space and improve speed. - The other shelf is a general-purpose shelf for objects, including
String.
String goes on the general shelf, so you use:
Array<String>
not a special StringArray.
Another way to picture it:
IntArrayis like a custom tray made only for integers.Array<String>is like a labeled box that can hold string objects.
Because strings are objects, they use the generic box.
Syntax and Examples
Basic syntax
Declare an array of strings
val names: Array<String>
Create an array of strings
val names = arrayOf("Alice", "Bob", "Charlie")
This creates an Array<String>.
Access elements
val first = names[0]
println(first)
Modify elements
names[1] = "Ben"
println(names.joinToString())
Full example
fun main() {
val fruits: Array<String> = arrayOf("Apple", "Banana", "Orange")
println(fruits[0])
fruits[1] = "Mango"
for (fruit fruits) {
println(fruit)
}
}
Step by Step Execution
Consider this example:
fun main() {
val names = arrayOf("Ana", "Ben", "Cara")
names[1] = "Blake"
println(names[0])
println(names.joinToString(" | "))
}
Here is what happens step by step:
arrayOf("Ana", "Ben", "Cara")creates anArray<String>with 3 elements.names[1] = "Blake"replaces the second element.- Arrays are zero-indexed.
- Index
1refers toBen.
println(names[0])prints the first element, which isAna.println(names.joinToString(" | "))joins all array elements into one string.
Final array contents:
["Ana", "Blake", "Cara"]
Output:
Real World Use Cases
Arrays of strings are useful when you have a fixed-size collection of text values.
Common examples
- Command-line arguments
- Kotlin
mainfunctions often receiveArray<String>
- Kotlin
- Static configuration values
- predefined role names, country codes, or category labels
- File or CSV processing
- one row may be temporarily handled as an array of strings
- Interoperating with Java APIs
- many Java methods expect
String[]
- many Java methods expect
- Test data
- storing sample names, inputs, or expected values
Example: command-line args
fun main(args: Array<String>) {
println("Received ${args.size} arguments")
for (arg in args) {
println(arg)
}
}
Here, args is exactly the Kotlin equivalent of Java's String[] args.
Real Codebase Usage
In real Kotlin projects, developers use Array<String> mostly in specific situations, not as the default collection type.
Common patterns
1. Java interoperability
When calling Java libraries, you may need Array<String> because the Java API expects String[].
val names = arrayOf("A", "B", "C")
2. Command-line entry points
fun main(args: Array<String>) {
if (args.isEmpty()) {
println("Please provide a filename.")
return
}
println("Reading: ${args[0]}")
}
This uses a guard clause to stop early if input is missing.
3. Validation before access
Because arrays are indexed, developers often check size first.
if (names.size > 2) {
println(names[2])
}
Common Mistakes
1. Looking for a StringArray type
This is the most common confusion.
Incorrect
val names: StringArray = ???
There is no built-in StringArray type in Kotlin.
Correct
val names: Array<String> = arrayOf("Alice", "Bob")
2. Confusing Array<String> with List<String>
They are different types.
val names = arrayOf("Alice", "Bob")
This is an array, not a list.
A list looks like this:
val names = listOf("Alice", "Bob")
Use arrays when you specifically need arrays. Use lists for most general-purpose Kotlin programming.
3. Accessing an invalid index
Broken code
Comparisons
Array<String> vs related Kotlin types
| Type | Use for | Fixed size | Holds nulls? | Notes |
|---|---|---|---|---|
Array<String> | Array of strings | Yes | No, unless declared nullable | Kotlin equivalent of Java String[] |
Array<String?> | Array of nullable strings | Yes | Yes | Useful when elements may be missing |
List<String> | Read-only list of strings | Usually fixed interface, implementation varies | No, unless nullable type used | Preferred for many Kotlin operations |
Cheat Sheet
// Java
String[] names = {"Alice", "Bob"};
// Kotlin equivalent
val names: Array<String> = arrayOf("Alice", "Bob")
Key rules
- Kotlin equivalent of Java
String[]isArray<String> - There is no built-in
StringArray - Special array types like
IntArrayexist only for primitive types - Arrays are fixed size
- Arrays are zero-indexed
Common syntax
val a = arrayOf("x", "y", "z")
val first = a[0]
a[1] = "new"
println(a.size)
println(a.joinToString())
Nullable string array
val a = arrayOfNulls<String>(3) // type: Array<String?>
Create by size
FAQ
Is there a StringArray type in Kotlin?
No. Kotlin uses Array<String> for arrays of strings.
What is the direct Kotlin equivalent of Java String[]?
It is Array<String>.
Why does Kotlin have IntArray but not StringArray?
IntArray is a specialized primitive array for performance. String is an object type, so it uses the generic Array<T> form.
Should I use Array<String> or List<String> in Kotlin?
Use Array<String> when an API requires an array or when fixed-size indexed storage is appropriate. Use List<String> for most general Kotlin code.
Can Array<String> contain null values?
Not unless you declare it as Array<String?>.
How do I create an empty string array in Kotlin?
Mini Project
Description
Build a small command-line program that stores a fixed list of names in a Kotlin string array and prints useful information about them. This project demonstrates how Array<String> works in a realistic way: creation, indexing, updating values, looping, and formatting output.
Goal
Create a Kotlin program that uses Array<String> to store names, update one element, and print the results clearly.
Requirements
- Create an
Array<String>with at least three names. - Print the first name in the array.
- Replace one of the names with a new value.
- Loop through the array and print every name.
- Print the full array as a single formatted string.
Keep learning
Related questions
Accessing Kotlin Extension Functions from Java
Learn how Kotlin extension functions are compiled and how to call them correctly from Java with clear examples and common pitfalls.
Android AlarmManager Example: Scheduling Tasks with AlarmManager
Learn how to use Android AlarmManager to schedule tasks, set alarms, and handle broadcasts with a simple beginner example.
Android Foreground Service Notification Channels in Kotlin
Learn why startForeground fails on Android 8.1 and how to create a valid notification channel for foreground services in Kotlin.