skip to content

Vararg & Spread Operator

vararg accepts any number of arguments and hands you an array, and the spread operator * passes an existing array into such a parameter. Interviewers note that spread copies the array, which matters in hot paths.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

questions

5

What does the `vararg` modifier do in a Kotlin function parameter, and how do you read its values inside the function body?

level: juniorimportance: must knowfreq 70%

answer

  1. vararg = variable number of args of one type
  2. Inside body it is an Array (IntArray for Int)
  3. Only one vararg per function
  4. Zero args = empty array, never null
  5. Params after vararg must be named

basics

~10 s

vararg lets a function accept any number of arguments of one type. You call it like f(1, 2, 3) and inside the function you read them as an array you can loop over.

solid answer

~30 s

A parameter marked `vararg` accepts a variable number of arguments of the same type. The caller passes zero, one, or many comma-separated values: `printAll("a", "b", "c")`. Inside the function the parameter is exposed as an `Array<T>` (for object types) or the specialized primitive array like `IntArray` for `vararg x: Int`. You iterate it with a `for` loop, read `.size`, index it, etc. A function may declare only one `vararg` parameter. If it is not the last parameter, any parameters after it must be passed using named arguments. Passing zero arguments yields an empty array, not null.

code

kotlin · 8 lines
kotlin
fun sum(vararg nums: Int): Int {  // nums is IntArray
    var total = 0
    for (n in nums) total += n
    return total
}

println(sum(1, 2, 3)) // 6
println(sum())        // 0

go deeper

for a junior

Knows vararg accepts many args and is read as an array via a for loop.

for a middle

Adds the one-vararg rule, primitive-array specialization, and that later params must be named.

for a senior

Explains empty-array (not null) semantics and JVM varargs mapping driving the Array type.

for a principal

Frames API design trade-offs: vararg vs List parameter for ergonomics, ABI, and overload resolution.

## What `vararg` is `vararg` (variable arity) is a parameter modifier that lets a single function accept **any number of arguments** of a given type, supplied as a comma-separated list at the call site. ```kotlin fun printAll(vararg items: String) { for (item in items) println(item) println("count = ${items.size}") } printAll("a", "b", "c") // count = 3 printAll() // count = 0 (empty array, NOT null) ``` ## How it is exposed inside the body Inside the function, the `vararg` parameter is an **array**: - For reference types `vararg items: String` → `Array<String>`. - For primitive types it becomes the specialized primitive array to avoid boxing: `vararg x: Int` → `IntArray`, `vararg x: Long` → `LongArray`, `vararg x: Double` → `DoubleArray`, etc. You use normal array operations: `for`, `items.size`, `items[i]`, `items.isEmpty()`, `items.joinToString()`. ## Rules - A function can have **at most one** `vararg` parameter. - The `vararg` parameter is usually last. If it is **not** last, every parameter declared **after** it must be supplied via **named arguments** at the call site (because positional binding would be ambiguous). - Passing no values produces an **empty array**, never `null`. ```kotlin fun tag(vararg classes: String, id: String) { /* ... */ } tag("a", "b", id = "main") // id MUST be named ``` ## Why arrays (not List) `vararg` maps to a JVM varargs array, so the parameter type is `Array`/`IntArray`, not `List`. If you want a list, call `.toList()` or `.asList()` inside.

  • What type is `vararg x: Int` inside the function?
    `IntArray` — a specialized primitive array, used instead of `Array<Int>` to avoid boxing.
  • Can a function have two vararg parameters?
    No. A function may declare at most one vararg parameter.

Like a checkout lane that accepts a basket with any number of items rather than a fixed number of slots.

saying these in an interview costs you the question

  • Saying the parameter is a `List` inside the body (it is an Array)
  • Thinking passing no arguments gives `null`
  • Claiming you can have multiple vararg params
  • Not knowing primitive varargs use IntArray/LongArray etc.

context

open as a page

You already have an `Array<String>`. How do you pass it into a function whose parameter is `vararg s: String`? What is the spread operator and what exactly does it do?

level: middleimportance: must knowfreq 65%

basics

~10 s

Put a * before the array when you call the function: printAll(*myArray). The * unpacks the array so each element is passed as a separate argument instead of one array argument.

open as a page

Can you mix a spread array with individual literal arguments in a single call to a vararg function? Show what the callee receives and in what order.

level: middleimportance: should knowfreq 45%

basics

~10 s

Yes. In one call you can write individual values and spread arrays together, like f(0, *arr, 9). Kotlin combines them in the exact left-to-right order you wrote them into one array for the function.

open as a page

Does spreading an array into a vararg parameter copy it or pass the same array? Explain the aliasing/mutation implications, including when the vararg parameter type is `Array<T>` of objects.

level: seniorimportance: should knowfreq 30%

basics

~20 s

Spreading copies the array's elements into a new array that the function receives, so the function's array is a different array. But for object elements both arrays point at the same objects, so mutating an object is visible to both.

open as a page

Explain how Kotlin `vararg` maps to JVM varargs for interop, how spread interacts with calling Java varargs methods, and one subtlety of overload resolution between a vararg overload and a fixed-arity overload.

level: principalimportance: nice to knowfreq 20%

basics

~20 s

Kotlin vararg compiles to a normal JVM varargs array, so Java code can call it and Kotlin can call Java varargs the same way. To pass an existing array to either, use the * spread. When both a vararg and an exact fixed overload match, Kotlin prefers the more specific fixed-arity one.

open as a page