• +91 9723535972
  • info@interviewmaterial.com

Kotlin Interview Questions and Answers

Question - How to ensure null safety in Kotlin?

Answer -

One of the major advantages of using Kotlin is null safety. In Java, if you access some null variable then you will get a NullPointerException. So, the following code in Kotlin will produce a compile-time error:

var name: String = "MindOrks"
name = null //error
So, to assign null values to a variable, you need to declare the name variable as a nullable string and then during the access of this variable, you need to use a safe call operator i.e. ?.

var name: String? = "MindOrks"
print(name?.length) // ok
name = null // ok

Comment(S)

Show all Coment

Leave a Comment




NCERT Solutions

 

Share your email for latest updates

Name:
Email:

Our partners