Junior — Middle
Can you explain what a lambda function is and how it is used?
sobes.tech AI
Answer from AI
A lambda function is an anonymous function, meaning a function without a name that can be defined directly at the point of use. In Android (Java or Kotlin), lambdas are often used to simplify code, especially when working with interfaces that have a single method (functional interfaces).
Example in Kotlin:
val sum = { a: Int, b: Int -> a + b }
println(sum(3, 4)) // Outputs 7
Lambdas are convenient for passing small blocks of code as parameters, such as event handlers or callbacks, making the code more compact and readable.