0
0
Kotlinprogramming~10 mins

SAM conversions for Java interfaces in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - SAM conversions for Java interfaces
Java Interface with Single Abstract Method
Kotlin Lambda Expression
SAM Conversion Applied
Instance of Java Interface Created
Interface Method Called
Kotlin converts a lambda into an instance of a Java interface with one abstract method (SAM) automatically when needed.
Execution Sample
Kotlin
val runnable = Runnable { println("Running") }
runnable.run()
Creates a Runnable using a lambda via SAM conversion and calls its run method.
Execution Table
StepActionEvaluationResult
1Define lambda for RunnableRunnable { println("Running") }Lambda stored as SAM instance
2Assign lambda to 'runnable'val runnable = Runnable { ... }runnable holds SAM instance
3Call runnable.run()runnable.run()Prints: Running
4End of execution--
💡 Program ends after runnable.run() prints output
Variable Tracker
VariableStartAfter 1After 2Final
runnablenullSAM instance wrapping lambdaSAM instance wrapping lambdaSAM instance wrapping lambda
Key Moments - 2 Insights
Why can we assign a lambda to a Java interface variable?
Because Kotlin automatically converts the lambda to an instance of the Java interface with a single abstract method, as shown in execution_table step 2.
What happens when runnable.run() is called?
The lambda code inside the SAM instance runs, printing 'Running' as shown in execution_table step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is stored in 'runnable' after step 2?
ANull
BA plain function
CA lambda expression as a SAM instance
DAn integer value
💡 Hint
Check execution_table row 2 under 'Result' column
At which step does the program print 'Running'?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at execution_table row 3 under 'Result' column
If the Java interface had two abstract methods, what would happen to the SAM conversion?
AKotlin would not apply SAM conversion automatically
BThe lambda would convert to a different interface
CIt would still work the same
DThe program would print an error at runtime
💡 Hint
SAM conversion applies only to interfaces with a single abstract method
Concept Snapshot
SAM conversions let Kotlin lambdas become Java interface instances
Only works if interface has exactly one abstract method
Syntax: val x = JavaInterface { lambda code }
Calling interface method runs the lambda
Simplifies using Java functional interfaces in Kotlin
Full Transcript
This visual trace shows how Kotlin converts a lambda expression into an instance of a Java interface with a single abstract method (SAM). First, a lambda is defined for the Runnable interface. Then Kotlin automatically wraps this lambda into a SAM instance and assigns it to the variable 'runnable'. When runnable.run() is called, the lambda code executes and prints 'Running'. This automatic conversion only works if the Java interface has exactly one abstract method. The variable tracker shows 'runnable' holds the SAM instance after assignment. The key moments clarify why the lambda can be assigned to the interface and what happens when the method is called. The quiz questions test understanding of these steps and the SAM conversion rule.