0
0
Kotlinprogramming~10 mins

Multiple type parameters in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multiple type parameters
Define generic class/function with <T, U>
Use T and U as types inside
Create instance with specific types
Access members using T and U types
Use or return values of types T and U
This flow shows how Kotlin uses multiple type parameters to create flexible generic classes or functions that work with different types.
Execution Sample
Kotlin
class Pair<T, U>(val first: T, val second: U)

fun main() {
  val p = Pair<Int, String>(1, "one")
  println("${p.first} and ${p.second}")
}
This code defines a generic Pair class with two type parameters and creates an instance with Int and String types.
Execution Table
StepActionEvaluationResult
1Define class Pair with type parameters T and UT and U are placeholdersClass ready to use with any types
2Call main functionStart executionEnter main
3Create Pair instance with T=Int, U=StringPair<Int, String>(1, "one")p.first = 1, p.second = "one"
4Print p.first and p.secondprintln("${p.first} and ${p.second}")Output: 1 and one
5End of mainProgram finishesExecution stops
💡 Program ends after printing the pair values
Variable Tracker
VariableStartAfter Step 3After Step 4Final
p.firstundefined111
p.secondundefined"one""one""one"
Key Moments - 3 Insights
Why do we write <T, U> in the class definition?
The <T, U> declares two type placeholders so the class can hold two different types. See execution_table step 1 where the class is defined with these placeholders.
How does Kotlin know what types T and U are?
When creating the instance (step 3), we specify the actual types Int and String, so Kotlin replaces T with Int and U with String.
What happens if we swap the types when creating Pair?
Swapping types changes what p.first and p.second hold. For example, Pair<String, Int>("one", 1) would reverse their types. This is shown by the type parameters in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the type of p.second?
AInt
BString
CDouble
DBoolean
💡 Hint
Check the 'Evaluation' column at step 3 where Pair is created
At which step does the program print the output?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the 'Print' action in the 'Action' column
If we change Pair<Int, String> to Pair<String, Int>, what will p.first be at step 3?
A"one"
B1
Cnull
DCompilation error
💡 Hint
Refer to the 'variable_tracker' and how type parameters map to values
Concept Snapshot
Multiple type parameters allow defining generic classes or functions with more than one type placeholder.
Syntax: class Pair<T, U>(val first: T, val second: U)
Use: Specify types when creating instances, e.g., Pair<Int, String>(1, "one")
This enables flexible, reusable code that works with different type combinations.
Full Transcript
This example shows how Kotlin uses multiple type parameters in a generic class called Pair. The class is defined with two placeholders, T and U, which represent any types. When we create an instance of Pair, we specify the actual types, like Int and String. The program then stores values of these types in the first and second properties. The execution table traces each step: defining the class, starting main, creating the Pair instance, printing the values, and ending the program. The variable tracker shows how p.first and p.second get their values. Key moments clarify why we use <T, U>, how Kotlin knows the types, and what happens if we swap them. The quiz tests understanding of types at different steps and the effect of changing type parameters. This helps beginners see how multiple type parameters work in practice.