Câu hỏi phỏng vấn Kotlin
Câu hỏi

What is SAM Conv...

Câu trả lời

SAM Conversion in Kotlin refers to the ability to use lambda expressions to implement interfaces with a single abstract method (SAM). This feature allows for more concise and readable code when working with functional interfaces[1][3].

Here's how it works:

  • To declare a functional interface in Kotlin, use the fun modifier before the interface keyword[3].
  • Instead of creating a class that implements the functional interface manually, you can use a lambda expression[3].
  • Kotlin will automatically convert the lambda expression into an implementation of the interface's single abstract method[1][3].

For example, consider this Kotlin functional interface[3]:

kotlin Copy
fun interface Runnable {
    fun run()
}

Without SAM conversion, you would need to write[3]:

kotlin Copy
val runnable = object : Runnable {
    override fun run() {
        println("Running...")
    }
}

With SAM conversion, you can simply use a l...

expert

expert

Gợi ý câu hỏi phỏng vấn

middle

What is a difference between a class and object in Kotlin?

middle

What are some disadvantages of Kotlin?

middle

How would you create a singleton with parameter in Kotlin?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào