The * operator is known as the Spread Operator in Kotlin.

From the Kotlin Reference:

When you call a vararg-function, you can pass arguments individually, for example asList(1, 2, 3). if you already have an array and want to pass its contents to the function, use the spread operator (prefix the array with *):

It can be applied to an Array before passing it into a function that accepts vararg.

For Example...

If you have a function that accepts a varied number of arguments...

fun sumOfNumbers(vararg numbers: Int): Int {
    return numbers.sum()
}

Use the spread operator to pass an array's elements as the arguments:

val numbers = intArrayOf(2, 3, 4)
val sum = sumOfNumbers(*numbers)
println(sum) // Prints '9'

Notes:

  • The * operator is also the multiplication operator (of course).
  • The operator can only be used when passing arguments to a function. The result of the operation cannot be stored since it yields no value (it is purely syntactic sugar).
  • The operator may confuse some C/C++ programmers at first because it looks like a pointer is being de-referenced. It isn't; Kotlin has no notion of pointers.
  • The operator can be used in-between other arguments when calling a vararg-function. This is demonstrated in the example here.
  • The operator is similar to the apply function in various functional programming languages.
Answer from byxor on Stack Overflow
Top answer
1 of 5
298

The * operator is known as the Spread Operator in Kotlin.

From the Kotlin Reference:

When you call a vararg-function, you can pass arguments individually, for example asList(1, 2, 3). if you already have an array and want to pass its contents to the function, use the spread operator (prefix the array with *):

It can be applied to an Array before passing it into a function that accepts vararg.

For Example...

If you have a function that accepts a varied number of arguments...

fun sumOfNumbers(vararg numbers: Int): Int {
    return numbers.sum()
}

Use the spread operator to pass an array's elements as the arguments:

val numbers = intArrayOf(2, 3, 4)
val sum = sumOfNumbers(*numbers)
println(sum) // Prints '9'

Notes:

  • The * operator is also the multiplication operator (of course).
  • The operator can only be used when passing arguments to a function. The result of the operation cannot be stored since it yields no value (it is purely syntactic sugar).
  • The operator may confuse some C/C++ programmers at first because it looks like a pointer is being de-referenced. It isn't; Kotlin has no notion of pointers.
  • The operator can be used in-between other arguments when calling a vararg-function. This is demonstrated in the example here.
  • The operator is similar to the apply function in various functional programming languages.
2 of 5
39

In addition to the answers that were directly towards "what is this thing!?!", you often have the case where you have a List and want to pass it to a function that is expecting a vararg. For this, the conversion is:

someFunc(x, y, *myList.toTypedArray())

Assuming that last parameter of someFunc is vararg of the same type as the elements in the list.

🌐
Kotlin
kotlinlang.org › docs › functions.html
Functions | Kotlin Documentation
When you call a vararg-function, you can pass arguments individually, as in the example of asList(1, 2, 3). If you already have an array and want to pass its contents to a function as a vararg parameter or as a part of it, use the spread operator by prefixing the array name with *:
Discussions

Help me understand the spread operator

Spread operator in Python and Kotlin are really much different things. Python lets you pass any positional arguments using an array. Kotlin lets you only pass vararg arguments.

And the reason why only arrays are supported is because vararg is internally represented as just array. So * in Kotlin does not really unpack anything. Actually, it is the opposite. If you declare a function with vararg then passed arguments are packed into an array. By using * you can pass an array directly.

More on reddit.com
🌐 r/Kotlin
10
21
July 26, 2021
Spread-object operator missing in kotlin? - Language Design - Kotlin Discussions
I was wondering if there is something like the spread-object operator found in Javascript/Typescript? I am aware that there is a spread function arguments operator. The spread object operator allows one to pass-on attribute values of objects of shared types without constructors. More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
1
June 1, 2018
Allocation of vararge with spread operator - Language Design - Kotlin Discussions
I used an Android game framework which had a vararge method. I called this method every frame. override fun update(){ foo(a,b,c) } I knew I don’t need a allocation here. So I fixed it with spread operator like I used to do in Java. val bar=arrayOf(a,b,c) //as a property override fun update(){ ... More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
0
October 6, 2016
Spread operator not working on vararg indexed access operator - Language Design - Kotlin Discussions
When using vararg as indexed access operator argument, we can only use explicit get function rather than square brackets. The following code shows this: class A { operator fun get(vararg index: Int): Int { r… More on discuss.kotlinlang.org
🌐 discuss.kotlinlang.org
1
September 5, 2017
People also ask

What does 'vararg' do in Kotlin?
The vararg keyword in Kotlin is used to define a function parameter that can accept a variable number of arguments. When a function has a vararg parameter, you can pass any number of values (zero or more) to that function, and those values are treated as an array within the function body. This provides a flexible way to handle function calls with a dynamic number of arguments.
🌐
dhiwise.com
dhiwise.com › post › the-kotlin-spread-operator-unfolding-arrays-into-arguments
Maximizing Efficiency with the Kotlin Spread Operator
How to pass array to varargs in Kotlin?
To pass an array to a function that accepts vararg parameters in Kotlin, you need to use the Kotlin spread operator, which is the asterisk *. By prefixing the array with the spread operator when making the function call, the array is expanded into individual elements, allowing Kotlin to interpret them as separate arguments. Example: ```kotlin fun printVarargs(vararg numbers: Int) { numbers.forEach { print("$it ") } } fun main() { val intArray = intArrayOf(1, 2, 3) printVarargs(*intArray) } ``` In the above example, *intArray uses the spread operator to pass the array elements i
🌐
dhiwise.com
dhiwise.com › post › the-kotlin-spread-operator-unfolding-arrays-into-arguments
Maximizing Efficiency with the Kotlin Spread Operator
How many varargs can we have in a function in Kotlin?
In Kotlin, a function can have only one vararg parameter, and it must be the last parameter if the function has other parameters. This restriction ensures that the compiler can clearly distinguish between the vararg values and the other arguments.
🌐
dhiwise.com
dhiwise.com › post › the-kotlin-spread-operator-unfolding-arrays-into-arguments
Maximizing Efficiency with the Kotlin Spread Operator
🌐
Baeldung
baeldung.com › home › kotlin › kotlin basics › varargs and spread operator in kotlin
Varargs and Spread Operator in Kotlin | Baeldung on Kotlin
April 28, 2026 - The “*” behind the numbers array variable is the spread operator. Under-the-hood, Kotlin translates its varargs to Java varargs.
🌐
DhiWise
dhiwise.com › post › the-kotlin-spread-operator-unfolding-arrays-into-arguments
Maximizing Efficiency with the Kotlin Spread Operator
June 6, 2024 - The spread operator kotlin proves its utility by streamlining the process of passing a list of values to a function that accepts a variable number of arguments (varargs). Not only does it enable you to pass an array as a series of arguments, ...
🌐
Reddit
reddit.com › r/kotlin › help me understand the spread operator
r/Kotlin on Reddit: Help me understand the spread operator
July 26, 2021 -

Hey all,

Been learning Kotlin (my background is Python)!
I was pretty happy to see that Kotlin has a very similar operation to unpack a collection of items (*).

That being said, I couldn't really find the exact reason why the spread operator only supports Arrays and not other types of collections like Lists? From my understanding, the main difference between List and and arrays is that Arrays are mutable whereas Lists are not by default (you'd have to call a mutableListOf), and arrays are fixed in size.

Where does this limitation come from?

Find elsewhere
🌐
Kotlin Discussions
discuss.kotlinlang.org › language design
Spread-object operator missing in kotlin? - Language Design - Kotlin Discussions
June 1, 2018 - I was wondering if there is something ... arguments operator. The spread object operator allows one to pass-on attribute values of objects of shared types without constructors....
🌐
Medium
proandroiddev.com › kotlins-vararg-and-spread-operator-4200c07d65e1
Kotlin’s vararg and spread operator | by Tibi Csabai | ProAndroidDev
January 27, 2019 - Kotlin has pointers?” was my ... not a pointer. It’s Kotlin’s spread operator — the operator that unpacks an array into the list of values from the array....
🌐
Commonsware
klassbook.commonsware.com › lessons › Collections › spread-operator-primitive.html
CommonsWare Klassbook: Spread Operator with Primitive Types
Assigning Function Results Chaining ... Functions Inheritance and Overriding Functions Local Variables Named Parameter Values override is Transitive Spread Operator Spread Operator with Primitive Types Values for Varargs Varargs...
🌐
Detekt
detekt.dev › performance rule set
Performance Rule Set | detekt
See more details here: Exploring Kotlin Hidden Costs - Part 1 Exploring Kotlin Hidden Costs - Part 2 Exploring Kotlin Hidden Costs - Part 3 · To solve this code smell, the forEach usage should be replaced by a for loop. ... In most cases using a spread operator causes a full copy of the array to be created before calling a method.
🌐
DeepSource
deepsource.com › directory › kotlin › issues › KT-P1004
Array constructors should be preferred over the spread operator (KT-P1004) ・ Kotlin
In most cases using a spread operator causes a full copy of the array to be created before calling a method. This has a very high performance penalty. The Kotlin compiler since v1.1.60 has an optimization that skips the array copy when an array constructor function is used to create the arguments that are passed to the vararg parameter.
🌐
Kotlin Discussions
discuss.kotlinlang.org › language design
Allocation of vararge with spread operator - Language Design - Kotlin Discussions
October 6, 2016 - I used an Android game framework which had a vararge method. I called this method every frame. override fun update(){ foo(a,b,c) } I knew I don’t need a allocation here. So I fixed it with spread operator like I used to do in Java. val bar=arrayOf(a,b,c) //as a property override fun update(){ ...
🌐
Medium
medium.com › @madananand › programming-kotlin-use-of-spread-operator-a620190d5f33
[Programming] Kotlin Use of Spread operator - Madan Anand - Medium
August 25, 2022 - [Programming] Kotlin Use of Spread operator Spread Operator “*” are the operator that unpacks an array into the list of values from the array. Mainly I used it for output operations. Because of …
🌐
DEV Community
dev.to › tedhagos › you-can-pass-variable-arguments-in-a-kotlin-function-3mnf
You can pass variable arguments in a Kotlin function - DEV Community
October 2, 2018 - (5) Like in Java, we can pass an array to a function that accepts variable arguments. We need to use spread operator * to unpack the array.
🌐
Commonsware
klassbook.commonsware.com › lessons › Collections › spread-operator.html
CommonsWare Klassbook: Spread Operator
If you already have an array that contains the values that you would want to pass to a vararg function, you can use the "spread operator" (*) as a prefix to the array. Then, instead of treating the array as a single "vararg", Kotlin will expand the array and consider each of its members to ...
🌐
Kotlin Discussions
discuss.kotlinlang.org › language design
Spread operator not working on vararg indexed access operator - Language Design - Kotlin Discussions
September 5, 2017 - When using vararg as indexed access operator argument, we can only use explicit get function rather than square brackets. The following code shows this: class A { operator fun get(vararg index: Int): Int { return 0 } } val a = A() val b = a[1, 2, 3] // OK val c = intArrayOf(1, 2, 3) val d = a.get(*c) // OK val e = a[c] //Error: Kotlin: Type mismatch: inferred type is IntArray but Int was expected val f = a[*c]/*Error: Kotlin: None of the following functions can be called with t...
🌐
GitHub
github.com › detekt › detekt › issues › 4860
Spread operator is shown as an issue even when using Kotlin 1.6.21 · Issue #4860 · detekt/detekt
May 25, 2022 - As per this Docs - https://detekt.dev/docs/rules/performance#spreadoperator, If we use Kotlin above version 1.1.60, the spread operator should not be a performance issue, Expected Behavior When using the spread operator in a project with...
Author   detekt
🌐
Wikipedia
en.wikipedia.org › wiki › TypeScript
TypeScript - Wikipedia
2 days ago - TypeScript (TS) is a high-level programming language that adds static typing with optional type annotations to JavaScript. It is designed for developing large applications. It transpiles to JavaScript. It is developed by Microsoft as free and open-source software released under an Apache License ...
🌐
Kotlin
kotlinlang.org › docs › arrays.html
Arrays | Kotlin Documentation
1 month ago - This is useful when you don't know the number of arguments in advance, like when formatting a message or creating an SQL query. To pass an array containing a variable number of arguments to a function, use the spread operator (*).
🌐
Kotlin Discussions
discuss.kotlinlang.org › t › hidden-allocations-when-using-vararg-and-spread-operator › 1640
Hidden allocations when using vararg and spread operator - Kotlin Discussions
May 1, 2016 - As I understand it, if I pass arguments to a vararg function like so: function("a", "b", "c", "d") a new Array will be created to hold them. But what about this: val someStringArray<String> = ... function(*someStringA…
🌐
GeeksforGeeks
geeksforgeeks.org › kotlin › passing-variable-arguments-to-a-function-in-kotlin
Passing Variable Arguments to a Function in Kotlin - GeeksforGeeks
January 27, 2023 - The spread operator, on the other hand, simply tells the compiler to unwrap array members and pass them as separate arguments, The spread operator-that is, *-is put just before the name of the array being passed in.