Star projection helps you work with generic types when you don't know the exact type. It lets you use a generic class safely without specifying the type.
When you want to accept any type of generic object without caring about its specific type.
When you call a function that uses generics but you don't know or need the exact type.
When you want to read from a generic collection but don't want to write to it.
When you want to avoid type errors while working with unknown generic types.
Syntax
Kotlin
val list: List<*> = listOf(1, "two", 3.0)
The star * means "some unknown type".
You can read elements as Any? but cannot add elements safely.
Examples
You can get elements as Any? but cannot add new elements.
This function accepts any list regardless of its element type.
Kotlin
fun printList(list: List<*>) {
for (item in list) {
println(item)
}
}
You can use star projection to hold mixed types safely.
Kotlin
val mixedList: List<*> = listOf("hello", 42, 3.14)
println(mixedList.size)
Sample Program
This program shows how to use star projection to print lists of unknown types safely.
Kotlin
fun printUnknownList(list: List<*>) {
for (item in list) {
println(item)
}
}
fun main() {
val numbers = listOf(1, 2, 3)
val words = listOf("apple", "banana")
val mixed = listOf(1, "two", 3.0)
printUnknownList(numbers)
printUnknownList(words)
printUnknownList(mixed)
}
OutputSuccess
Important Notes
You cannot add elements to a collection with star projection because the type is unknown.
Star projection is useful for reading data safely when you don't know the exact generic type.
Summary
Star projection * means an unknown generic type.
It allows safe reading but restricts writing to generic collections.
Use it when you want to work with generics without specifying exact types.
Practice
(1/5)
1. What does the star projection * represent in Kotlin generics?
easy
A. A type that can only be used with primitive data types
B. A specific generic type that must be defined explicitly
C. A wildcard that allows writing but restricts reading
D. An unknown generic type that allows safe reading but restricts writing
Solution
Step 1: Understand star projection meaning
The star projection * means an unknown generic type in Kotlin.
Step 2: Analyze its usage
It allows safe reading from generic collections but restricts writing to avoid type errors.
Final Answer:
An unknown generic type that allows safe reading but restricts writing -> Option D
Quick Check:
Star projection = unknown generic type with safe read [OK]
Hint: Star projection means unknown type, safe read only [OK]
Common Mistakes:
Thinking star projection allows writing to generics
Confusing star projection with specific generic types
Assuming star projection only works with primitives
2. Which of the following is the correct syntax to declare a list with star projection in Kotlin?
easy
A. val list: List<Any> = listOf(1, 2, 3)
B. val list: List<*> = listOf(1, 2, 3)
C. val list: List<> = listOf(1, 2, 3)
D. val list: List = listOf(1, 2, 3)
Solution
Step 1: Recall star projection syntax
Star projection uses * inside angle brackets to represent unknown type.
Step 2: Check each option
val list: List<*> = listOf(1, 2, 3) uses List<*> which is correct syntax for star projection.