# How to Initialize an Array in Kotlin with Values: Complete Guide

> Initialize an array in Kotlin with values using arrayOf(). Learn this efficient method for creating fixed-size arrays with automatic type inference and zero overhead.

- Repository: [JetBrains/kotlin](https://github.com/jetbrains/kotlin)
- Tags: how-to-guide
- Published: 2026-02-13

---

**Use the `arrayOf()` standard library function to initialize an array in Kotlin with values, which creates a fixed-size `Array<T>` with automatic type inference and zero runtime overhead due to its inline implementation.**

To initialize an array in Kotlin with values, you leverage the `arrayOf()` function provided by the Kotlin standard library. This approach works consistently across JVM, JavaScript, and Native targets, offering type-safe array creation without verbose syntax. The implementation resides in the JetBrains/kotlin repository, specifically within [`libraries/stdlib/src/kotlin/collections/Arrays.kt`](https://github.com/JetBrains/kotlin/blob/main/libraries/stdlib/src/kotlin/collections/Arrays.kt) and [`libraries/stdlib/src/kotlin/Array.kt`](https://github.com/JetBrains/kotlin/blob/main/libraries/stdlib/src/kotlin/Array.kt).

## Using `arrayOf()` to Initialize Arrays

The primary mechanism to initialize an array in Kotlin with values is the **`arrayOf()`** function. Defined in [`libraries/stdlib/src/kotlin/collections/Arrays.kt`](https://github.com/JetBrains/kotlin/blob/main/libraries/stdlib/src/kotlin/collections/Arrays.kt), this **inline** function delegates to platform-specific runtime constructors—such as `java.lang.reflect.Array.newInstance` on the JVM—to allocate an `Array<T>` while preserving Kotlin's type safety.

Because `arrayOf()` is inlined, the compiler inserts the array creation logic directly at the call site, eliminating function call overhead. The compiler also performs **type inference** based on the supplied arguments, removing the need for explicit generic parameters in most cases.

```kotlin
// Integer array with inferred type Array<Int>
val numbers = arrayOf(1, 2, 3, 4)

// String array
val fruits = arrayOf("Apple", "Banana", "Cherry")

// Explicit type for empty arrays
val emptyStrings = arrayOf<String>()

// Mixed types default to nearest common supertype (Any)
val mixed = arrayOf(42, "Answer", true)

// Custom object types
data class Point(val x: Int, val y: Int)
val points = arrayOf(Point(0, 0), Point(1, 2), Point(3, 4))

```

## Understanding the Array Class Architecture

The **`Array<T>`** class itself is defined in [`libraries/stdlib/src/kotlin/Array.kt`](https://github.com/JetBrains/kotlin/blob/main/libraries/stdlib/src/kotlin/Array.kt). This generic class represents a fixed-size collection of elements with properties like `size`, indexed access operators (`get` and `set`), and iterator support. When you call `arrayOf()`, the standard library delegates to platform-specific implementations while exposing a unified Kotlin API, ensuring consistent behavior across all supported targets.

## Converting Collections to Arrays

While `arrayOf()` creates arrays from literal values, you can also initialize an array from an existing collection using the **`toTypedArray()`** extension function. This approach is useful when working with Lists that need conversion to the fixed-size Array type.

```kotlin
val list = listOf(10, 20, 30)
val arrayFromList = list.toTypedArray()   // Results in Array<Int>

```

## Summary

- **`arrayOf()`** is the idiomatic way to initialize an array in Kotlin with values, implemented in [`libraries/stdlib/src/kotlin/collections/Arrays.kt`](https://github.com/JetBrains/kotlin/blob/main/libraries/stdlib/src/kotlin/collections/Arrays.kt)
- The function is **inline**, providing performance equivalent to direct array instantiation without call overhead
- **Type inference** automatically determines `Array<T>` from arguments, though explicit generic types work for empty arrays
- The `Array<T>` class resides in [`libraries/stdlib/src/kotlin/Array.kt`](https://github.com/JetBrains/kotlin/blob/main/libraries/stdlib/src/kotlin/Array.kt) and provides cross-platform compatibility through platform-specific runtime delegation
- Convert existing collections using **`toTypedArray()`** when initialization from literals is not required

## Frequently Asked Questions

### What is the difference between `arrayOf()` and the `Array` constructor?

The `Array` constructor requires a size and an initializer lambda (e.g., `Array(5) { it * 2 }`), making it suitable for computed values, while `arrayOf()` directly accepts pre-defined values as vararg parameters. According to the JetBrains/kotlin source code, `arrayOf()` is preferred for literal initialization because it provides cleaner syntax and superior type inference.

### Can I create an empty array in Kotlin?

Yes, invoke `arrayOf()` with an explicit type parameter but no arguments: `arrayOf<String>()`. This creates an empty `Array<T>` with zero length. The type parameter is required because the compiler cannot infer type information from an empty argument list.

### How do I initialize primitive arrays like `IntArray`?

For primitive arrays, use specialized factory functions such as **`intArrayOf()`**, **`doubleArrayOf()`**, or **`booleanArrayOf()`**, which are also defined in [`libraries/stdlib/src/kotlin/collections/Arrays.kt`](https://github.com/JetBrains/kotlin/blob/main/libraries/stdlib/src/kotlin/collections/Arrays.kt). These create optimized primitive arrays rather than boxed `Array<Int>` objects, improving memory efficiency on the JVM.

### Is `arrayOf()` efficient for large arrays?

Yes, `arrayOf()` incurs no performance penalty because it is an inline function. The Kotlin compiler expands the call directly into array allocation bytecode, identical to manual array creation. However, for very large datasets where elements follow a computable pattern, the `Array(size) { index -> ... }` constructor may be more memory-efficient than passing a large vararg to `arrayOf()`.