Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

The * token in Kotlin functions as a binary arithmetic operator for multiplication, a unary prefix spread operator for array unpacking, a star projection token within the generic type system, and a wildcard character in import directives.

Binary Arithmetic Operator (Multiplication)

As a binary operator, * performs arithmetic multiplication. Kotlin resolves this via operator overloading, mapping the * symbol to the times() member or extension function.
// Syntactic representation
val result = a * b

// Compiler translation
val result = a.times(b)
To enable the * operator for custom classes, the type must declare a function named times accompanied by the operator modifier. The operand type and return type are strictly defined by the function signature.
class Matrix {
    operator fun times(scalar: Int): Matrix {
        return Matrix()
    }
}

Unary Prefix Spread Operator

When positioned as a prefix immediately preceding an array identifier, * acts as the spread operator. It unpacks the elements of an array, allowing them to be passed as discrete arguments to a function parameter declared with the vararg modifier.
fun processElements(vararg elements: String) {
    // Function body
}

val stringArray = arrayOf("A", "B", "C")

// Applying the spread operator
processElements(*stringArray)
Mechanical Characteristics:
  • Type Restriction: The spread operator is strictly compatible with arrays (e.g., Array<T>, IntArray, DoubleArray). It cannot be applied directly to a Collection<T> (which requires conversion via .toTypedArray()) or a pure Iterable<T> (which requires conversion via .toList().toTypedArray()).
  • Memory Allocation and Mutation: Invoking the spread operator creates a shallow copy of the array elements. For object types, a vararg parameter compiles to Array<out T>, where the out variance inherently prevents element mutation at compile time. However, for primitive arrays (e.g., IntArray), element mutation is permitted. In these cases, the shallow copy ensures that any modifications to the array’s elements within the function do not affect the original array.
  • Argument Composition: The operator can be interleaved with standard, comma-separated arguments within the same function invocation.
processElements("Prefix", *stringArray, "Suffix")

Star Projection (Generics)

While structurally a type projection rather than an expression-level operator, * is utilized within angle brackets to denote an unknown type argument in parameterized types.
val heterogeneousList: List<*> = listOf(1, "Two", 3.0)
Mechanically, * provides a safe approximation of a generic type based on its variance declaration and upper bounds:
  • For a covariant type parameter (out T : TUpper), * projects to out TUpper, allowing safe reads of the upper bound. If no explicit upper bound is declared, it defaults to out Any?.
  • For a contravariant type parameter (in T), * projects to in Nothing, preventing unsafe writes to the generic instance.
  • For an invariant type parameter (T : TUpper), * projects to out TUpper for reading and in Nothing for writing.

Wildcard Import

In file headers, the * token acts as a wildcard within import directives. It instructs the compiler to import all accessible top-level declarations (classes, interfaces, functions, and properties) from a specified package or classifier, eliminating the need to import each member individually.
// Imports all members of java.util
import java.util.*

// Imports all nested declarations of a specific class or object
import org.example.Constants.*
Master Kotlin with Deep Grasping Methodology!Learn More