Question

Kotlin Equivalent of Java String[]: Arrays of Strings Explained

kotlinjava

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:
    • IntArray
    • DoubleArray
    • BooleanArray
    • and others
  • Object arrays use the generic Array<T> type:
    • Array<String>
    • Array<User>
    • Array<Any>

So Array<String> is the normal and correct way to represent an array of strings in Kotlin.

Why this matters

Understanding this helps you:

  • read Kotlin APIs correctly
  • convert Java code to Kotlin more easily
  • avoid searching for non-existent types like StringArray
  • choose between Array<String> and List<String> appropriately

Mental Model

Think of Kotlin arrays as two storage shelves:

  • One shelf is specially built for primitive values like Int and Double to 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:

  • IntArray is 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.

Take Quiz

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 in fruits) {
        println(fruit)
    }
}

What this does

  • Creates a string array with three values
  • Prints the first item
  • Replaces Banana with Mango
  • Loops through the array and prints each element

Creating an empty-sized string array

val words = arrayOfNulls<String>(3)

This creates an array that can hold three nullable strings:

Array<String?>

You can then assign values later:

words[0] = "Hello"
words[1] = "Kotlin"

Creating with a lambda

val labels = Array(3) { index -> "Item $index" }
println(labels.joinToString())

This produces:

Item 0, Item 1, Item 2

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:

  1. arrayOf("Ana", "Ben", "Cara") creates an Array<String> with 3 elements.
  2. names[1] = "Blake" replaces the second element.
    • Arrays are zero-indexed.
    • Index 1 refers to Ben.
  3. println(names[0]) prints the first element, which is Ana.
  4. println(names.joinToString(" | ")) joins all array elements into one string.

Final array contents:

["Ana", "Blake", "Cara"]

Output:

Ana
Ana | Blake | Cara

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 main functions often receive Array<String>
  • 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[]
  • 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.

Take Quiz

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])
}

4. Converting to collections

In many Kotlin codebases, developers prefer List<String> for most business logic.

val array = arrayOf("red", "green", "blue")
val list = array.toList()

5. Transforming values

You can use higher-order functions on arrays too.

val names = arrayOf("alice", "bob")
val upper = names.map { it.uppercase() }
println(upper)

This returns a List<String>, not an Array<String>.

Practical note

If you need a fixed-size structure or Java compatibility, Array<String> is a good fit. If you mainly want convenient Kotlin collection operations, List<String> is often better.

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

val names = arrayOf("Alice", "Bob")
println(names[2])

This throws an ArrayIndexOutOfBoundsException because valid indices are 0 and 1.

Better

if (names.size > 2) {
    println(names[2])
}

4. Forgetting nullability when using arrayOfNulls

Example

val words = arrayOfNulls<String>(2)

This is not Array<String>. It is:

Array<String?>

Elements may be null, so handle them safely.

5. Assuming arrays automatically resize

Kotlin arrays have a fixed size.

Broken idea

val names = arrayOf("Alice", "Bob")
// names.add("Cara") // not allowed

If you need a resizable structure, use MutableList<String> instead.

6. Forgetting that primitive arrays are special cases

Beginners sometimes expect all types to follow the SomethingArray pattern. That is not how Kotlin works.

  • IntArray exists because Int is primitive-like
  • StringArray does not exist because String is an object type

Comparisons

Array<String> vs related Kotlin types

TypeUse forFixed sizeHolds nulls?Notes
Array<String>Array of stringsYesNo, unless declared nullableKotlin equivalent of Java String[]
Array<String?>Array of nullable stringsYesYesUseful when elements may be missing
List<String>Read-only list of stringsUsually fixed interface, implementation variesNo, unless nullable type usedPreferred for many Kotlin operations
MutableList<String>Resizable list of stringsNoNo, unless nullable type usedGood when adding/removing items

Array<String> vs primitive arrays

Kotlin typeJava equivalentSpecialized?
IntArrayint[]Yes
DoubleArraydouble[]Yes
CharArraychar[]Yes
Array<String>String[]No
Array<User>User[]No

When to choose what

  • Choose Array<String> when:
    • you need Java array compatibility
    • you have fixed-size indexed data
    • an API requires an array
  • Choose List<String> when:
    • you are doing general Kotlin application logic
    • you want more collection-friendly operations
  • Choose MutableList<String> when:
    • you need to add or remove strings dynamically
Take Quiz

Cheat Sheet

// Java
String[] names = {"Alice", "Bob"};

// Kotlin equivalent
val names: Array<String> = arrayOf("Alice", "Bob")

Key rules

  • Kotlin equivalent of Java String[] is Array<String>
  • There is no built-in StringArray
  • Special array types like IntArray exist 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

val a = Array(3) { i -> "Item $i" }

Convert

val list = a.toList()

Remember

  • Array<String> = object array
  • IntArray = specialized primitive array
  • Use List<String> more often in regular Kotlin code

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?

You can use:

val names = emptyArray<String>()

How do I create a fixed-size nullable string array?

Use:

val names = arrayOfNulls<String>(5)

This gives you an Array<String?>.

Take Quiz

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

  1. Create an Array<String> with at least three names.
  2. Print the first name in the array.
  3. Replace one of the names with a new value.
  4. Loop through the array and print every name.
  5. Print the full array as a single formatted string.
Take Quiz

Keep learning

Related questions

View more