Home > front end >  What is the difference between two line of code in Kotlin?
What is the difference between two line of code in Kotlin?

Time:01-14

I know what is the ?, !!, or == in kotlin I really confuse the exact difference between them, below I have line of code, what is the difference between both line of code?

 users.find { it.id != userId }?.name 

 users.find { it.id == userId }!!.name

CodePudding user response:

== operator is used to check whether the contents of 2 variables match example the user.id and it.id in your code.

!= is used when we want to check whether the contents do not match they are opposite to each other.

Update after edit - Assuming the users is a list or one of the kotlin collection the first LOC finds the first user from the collection that has same id as userId then if there is such a user ie the ? returns some object then gets the name of same.

The second LOC has a condition that is exactly opposite it finds the first user that does not have the same id and provides its name.

  •  Tags:  
  • Related