• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Question - Why would you use apply in Kotlin?

Answer -

Look at this code:

person.name = "Tony Stark"
person.age = 52
and equivalent with apply will be:

val person = Person().apply {
    name = "Tony Stark" // this. can be omitted
    age = 52 // this. can be omitted
    // ...
}
This way you don't have to repeat person several times. Apply is used to keep things that belong together in one place (mostly initializations).

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners