• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Question - What is the difference between a safe calls(?.) and a null check(!!) in Kotlin?

Answer -

Difference between safe calls(?.) and a null check(!!) in Kotlin:

The safe call operator i.e. ?. is used to check if the variable's value is null or not. If it is null, then null will be returned otherwise it will return the desired value.

var name: String? = "JavaTpoint"  
println(name?.length) // 10  
name = null  
println(name?.length) // null  
If you want to throw NullPointerException when the variable's value is null, you can use the null check or !! Operator.

See the example:

var name: String? = "JavaTpoint"  
println(name?.length) // 10  
name = null  
println(name!!.length) // KotlinNullPointerException  

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners