Kotlin
Java
String Array
Programming
Language Conversion

What's the Kotlin equivalent of Java's String?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

This question usually means “What is the Kotlin equivalent of Java’s String[]?” In Kotlin, the direct equivalent is Array<String>, and in many cases you create it with arrayOf(...).

Java String[] vs Kotlin Array<String>

In Java, a string array looks like this:

java
String[] names = {"Alice", "Bob", "Cara"};

In Kotlin, the equivalent looks like this:

kotlin
val names: Array<String> = arrayOf("Alice", "Bob", "Cara")

That is the core mapping:

  • Java String[]
  • Kotlin Array<String>

The two are conceptually the same kind of structure: a fixed-size array containing string elements.

The Most Common Kotlin Forms

The most common way to create a string array in Kotlin is arrayOf(...).

kotlin
val fruits = arrayOf("Apple", "Banana", "Cherry")
println(fruits.joinToString())

You can also declare the type explicitly:

kotlin
val colors: Array<String> = arrayOf("Red", "Green", "Blue")

Or create an array by size:

kotlin
val placeholders = Array(3) { "Default" }
println(placeholders.joinToString())

This creates a fixed-size array with three string elements initialized through the lambda.

Access and Mutation Work the Same Way

Array indexing feels very similar to Java:

kotlin
1val names = arrayOf("Alice", "Bob", "Cara")
2
3names[1] = "Ben"
4
5println(names[0])
6println(names[1])

So if you are moving from Java to Kotlin, the main change is the type syntax, not the basic array behavior.

The size remains fixed, just like a Java array. If you need a collection that grows and shrinks, you probably want a list rather than an array.

Arrays vs Lists in Kotlin

This is where Kotlin differs more in style than in capability. Kotlin code often prefers List<String> over Array<String> unless a real array is needed.

For example:

kotlin
val names = listOf("Alice", "Bob", "Cara")
println(names)

Why does this matter?

Because in Kotlin:

  • arrays are fixed-size indexed containers
  • lists are the more common high-level collection type

So if you are translating Java literally, String[] becomes Array<String>. But if you are writing idiomatic Kotlin from scratch, List<String> is often the more natural choice.

Interoperability With Java

Kotlin interoperates smoothly with Java arrays. If a Java method expects String[], Kotlin can pass Array<String> naturally in many cases.

kotlin
1fun printAll(values: Array<String>) {
2    for (value in values) {
3        println(value)
4    }
5}
6
7val items = arrayOf("one", "two", "three")
8printAll(items)

This is one reason the Array<String> type exists so directly. Kotlin needs a clear representation that maps cleanly to Java arrays.

Nullability Matters

Kotlin also makes nullability explicit, which is something Java arrays do not express in the type system as clearly.

An array of non-null strings:

kotlin
val safeNames: Array<String> = arrayOf("Alice", "Bob")

An array where elements may be null:

kotlin
val maybeNames: Array<String?> = arrayOf("Alice", null, "Cara")

This is an important Kotlin advantage. The type tells you whether null elements are allowed.

Varargs and Command-Line Arguments

One place Java developers often see string arrays in Kotlin is main:

kotlin
fun main(args: Array<String>) {
    println(args.joinToString())
}

That is the Kotlin equivalent of Java’s public static void main(String[] args).

So even though Kotlin often prefers lists in application code, arrays are still normal and important in interop and entry-point scenarios.

Common Pitfalls

The most common pitfall is translating Java String[] mechanically without asking whether Kotlin List<String> would actually be the better choice for the new code.

Another mistake is forgetting the type syntax and writing Java-style array declarations in Kotlin. Kotlin uses Array<String>, not String[] in type declarations.

A third issue is overlooking nullability. Array<String> and Array<String?> are different types with different guarantees.

Finally, some developers assume Kotlin arrays are dynamically resizable because the language has rich collection APIs. They are not. Arrays stay fixed-size.

Summary

  • The Kotlin equivalent of Java String[] is Array<String>.
  • The most common creation syntax is arrayOf("a", "b", "c").
  • Arrays in Kotlin are fixed-size, just like Java arrays.
  • In idiomatic Kotlin, List<String> is often preferred when array behavior is not required.
  • Kotlin makes string-array nullability explicit through types such as Array<String?>.

Course illustration
Course illustration

All Rights Reserved.