0
0
Kotlinprogramming~10 mins

Collections interop behavior in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Collections interop behavior
Start with Kotlin Collection
Pass to Java Method
Java sees Collection as Java Collection
Java modifies or reads Collection
Back to Kotlin with updated Collection
Kotlin uses Collection with Java changes
Shows how Kotlin collections are passed to Java and seen as Java collections, then changes flow back to Kotlin.
Execution Sample
Kotlin
fun main() {
    val list = mutableListOf("a", "b")
    JavaInterop.modifyList(list)
    println(list)
}
Kotlin mutable list passed to Java method which modifies it, then Kotlin prints updated list.
Execution Table
StepActionKotlin list contentJava viewOutput
1Create Kotlin mutableListOf("a", "b")["a", "b"]N/AN/A
2Pass list to JavaInterop.modifyList["a", "b"]Java List with ["a", "b"]N/A
3JavaInterop adds "c" to list["a", "b", "c"]Java List after add: ["a", "b", "c"]N/A
4Return to Kotlin["a", "b", "c"]N/AN/A
5Print list in Kotlin["a", "b", "c"]N/A[a, b, c]
6End["a", "b", "c"]N/AN/A
💡 Java modifies the Kotlin list, changes visible back in Kotlin, then program ends.
Variable Tracker
VariableStartAfter Java modifiesFinal
list["a", "b"]["a", "b", "c"]["a", "b", "c"]
Key Moments - 2 Insights
Why does the Kotlin list show the new element added by Java?
Because Kotlin mutableListOf returns a mutable list that Java sees as a normal Java List, so changes in Java affect the same object Kotlin uses (see execution_table step 3 and 4).
Does Kotlin copy the list when passing to Java?
No, Kotlin passes the actual list object to Java, so both share the same collection instance (execution_table step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the content of the list after JavaInterop.modifyList adds an element?
A["a", "b", "c"]
B["a", "b"]
C["c"]
D[]
💡 Hint
Check execution_table row 3 under Kotlin list content.
At which step does Kotlin print the updated list?
AStep 2
BStep 5
CStep 3
DStep 1
💡 Hint
Look at execution_table row 5 under Action and Output.
If Kotlin passed an immutable list to Java, what would happen to the list content after Java tries to add an element?
AKotlin would throw an error immediately
BThe list would have the new element added
CThe list would remain unchanged in Kotlin
DJava would receive a copy and modify only that
💡 Hint
Think about mutability and shared references shown in variable_tracker.
Concept Snapshot
Kotlin collections passed to Java are seen as Java collections.
Mutable Kotlin collections can be changed by Java code.
Changes in Java reflect back in Kotlin because they share the same object.
Immutable Kotlin collections prevent Java from modifying them.
Interop uses shared references, not copies.
Full Transcript
This example shows how Kotlin collections behave when passed to Java code. A Kotlin mutable list is created with two elements. This list is passed to a Java method that adds a new element. Because Kotlin mutable lists are seen as Java lists, the Java method modifies the same list object. When control returns to Kotlin, the list now contains the new element added by Java. Printing the list in Kotlin shows all three elements. This demonstrates that Kotlin and Java share the same collection instance during interop, so changes in one language are visible in the other. If the Kotlin list were immutable, Java could not modify it. This behavior is important to understand when mixing Kotlin and Java code.