0
0
Kotlinprogramming~10 mins

Star projection for unknown types in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Star projection for unknown types
Start with generic type T
Unknown type encountered
Apply star projection (*)
Use projected type safely
Compile and run without type errors
When Kotlin code encounters an unknown generic type, it uses star projection (*) to safely handle it without knowing the exact type.
Execution Sample
Kotlin
fun printList(list: List<*>) {
    for (item in list) {
        println(item)
    }
}

val mixedList: List<Any> = listOf(1, "two", 3.0)
printList(mixedList)
This code prints all items in a list of unknown type using star projection.
Execution Table
StepActionType of list parameterItem type inside loopOutput
1Call printList with mixedListList<*> (star projection)Unknown (star projected)
2Enter for loop, get first itemList<*>Any? (1)1
3Print first itemList<*>Any? (1)1
4Get second itemList<*>Any? ("two")two
5Print second itemList<*>Any? ("two")two
6Get third itemList<*>Any? (3.0)3.0
7Print third itemList<*>Any? (3.0)3.0
8End of list reachedList<*>N/ALoop ends
💡 All items printed; star projection allowed safe iteration without knowing exact type.
Variable Tracker
VariableStartAfter 1After 2After 3Final
listList<Any> (mixedList)List<*>List<*>List<*>List<*>
itemN/A1 (Int)"two" (String)3.0 (Double)N/A
Key Moments - 2 Insights
Why can we use List<*> instead of List<T> when the type is unknown?
List<*> means the type is unknown but safe to use; it lets us read items as Any? without type errors, as shown in execution_table rows 1-7.
Can we add items to a List<*>?
No, because the exact type is unknown, adding items is unsafe. The star projection only allows safe reading, not writing.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the type of the list parameter?
AList<Any>
BList<String>
CList<*>
DList<Int>
💡 Hint
Check the 'Type of list parameter' column at step 2 in execution_table.
At which step does the loop finish iterating over all items?
AStep 6
BStep 8
CStep 7
DStep 5
💡 Hint
Look for the 'End of list reached' action in execution_table.
If we tried to add an item to the list inside printList, what would happen?
AIt would cause a compile-time error.
BIt would compile and add the item safely.
CIt would add the item but cause runtime error.
DIt would ignore the add operation.
💡 Hint
Recall key_moments about adding items to List<*>.
Concept Snapshot
Star projection (*) in Kotlin means unknown generic type.
Use List<*> to handle lists when type is unknown.
You can read items as Any?, but cannot add items.
Safe way to work with generics without exact type.
Common in APIs and libraries for flexibility.
Full Transcript
This visual trace shows how Kotlin uses star projection (*) to handle unknown generic types safely. When a function takes a List<*>, it means the list's element type is unknown. The code can safely read items as Any? but cannot add new items. The execution table walks through calling printList with a mixed list of Int, String, and Double. Each item is printed without type errors. The variable tracker shows how the list parameter is treated as List<*> and items are read one by one. Key moments clarify why star projection allows safe reading but forbids adding. The quiz tests understanding of the type at each step and the limitations of star projection. The snapshot summarizes star projection as a safe way to handle unknown generic types in Kotlin.