Bird
Raised Fist0
Kotlinprogramming~10 mins

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

Choose your learning style10 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.

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

  1. Step 1: Understand star projection meaning

    The star projection * means an unknown generic type in Kotlin.
  2. Step 2: Analyze its usage

    It allows safe reading from generic collections but restricts writing to avoid type errors.
  3. Final Answer:

    An unknown generic type that allows safe reading but restricts writing -> Option D
  4. 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

  1. Step 1: Recall star projection syntax

    Star projection uses * inside angle brackets to represent unknown type.
  2. Step 2: Check each option

    val list: List<*> = listOf(1, 2, 3) uses List<*> which is correct syntax for star projection.
  3. Final Answer:

    val list: List<*> = listOf(1, 2, 3) -> Option B
  4. Quick Check:

    Star projection syntax = List<*> [OK]
Hint: Star projection syntax always uses * inside angle brackets [OK]
Common Mistakes:
  • Leaving angle brackets empty like List<>
  • Using List<Any> which is not star projection
  • Confusing star projection with out-projection
3. What will be the output of the following Kotlin code?
fun printFirst(list: List<*>) {
    println(list[0])
}

fun main() {
    val numbers: List<Int> = listOf(10, 20, 30)
    printFirst(numbers)
}
medium
A. 10
B. Compilation error
C. null
D. Runtime exception

Solution

  1. Step 1: Understand star projection usage in function

    The function accepts List<*>, meaning unknown generic type, safe to read elements.
  2. Step 2: Analyze the call with List<Int>

    Passing List<Int> is valid; reading list[0] returns 10 safely.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    Reading from List<*> returns element safely [OK]
Hint: Reading from List<*> works, writing does not [OK]
Common Mistakes:
  • Assuming star projection causes compile error
  • Expecting null instead of actual element
  • Thinking runtime exception occurs on read
4. Identify the error in the following Kotlin code using star projection:
fun addElement(list: MutableList<*>, element: Any) {
    list.add(element)
}
medium
A. Element type Any is incompatible with list
B. Syntax error in function declaration
C. Cannot add element to MutableList<*> due to unknown type
D. No error, code compiles and runs fine

Solution

  1. Step 1: Understand star projection with MutableList

    MutableList<*> means unknown generic type; writing is restricted to avoid type errors.
  2. Step 2: Analyze the add operation

    Trying to add element causes compile error because type is unknown and unsafe to write.
  3. Final Answer:

    Cannot add element to MutableList<*> due to unknown type -> Option C
  4. Quick Check:

    Writing to MutableList<*> causes error [OK]
Hint: Cannot write to star-projected mutable collections [OK]
Common Mistakes:
  • Thinking add works with MutableList<*>
  • Confusing syntax error with type error
  • Assuming Any type is always accepted
5. Given a function that accepts a list with star projection, how can you safely copy elements from List<*> to a MutableList<Any> in Kotlin?
hard
A. Use a loop to cast each element to Any and add to MutableList<Any>
B. Directly assign List<*> to MutableList<Any>
C. Use star projection on both lists
D. Use MutableList<*> instead of MutableList<Any>

Solution

  1. Step 1: Understand star projection restrictions

    List<*> allows safe reading but elements are of unknown type, so direct assignment to MutableList<Any> is unsafe.
  2. Step 2: Safely copy elements

    Use a loop to read each element and cast to Any before adding to MutableList<Any>, ensuring type safety.
  3. Final Answer:

    Use a loop to cast each element to Any and add to MutableList<Any> -> Option A
  4. Quick Check:

    Copy with loop and cast = safe transfer [OK]
Hint: Copy elements with loop and cast from List<*> to MutableList<Any> [OK]
Common Mistakes:
  • Trying direct assignment between incompatible generic types
  • Using star projection on mutable list incorrectly
  • Ignoring type casting when copying elements
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard