What is null/nil in Kotlin and how to work with it?
Hi everyone! I started learning Kotlin after Java and can't fully understand the concept of null in this language. In Java, null is a regular reference that can be assigned to any object, and we constantly struggle with NullPointerException. In Kotlin, as I understand, types are non-null by default, and special constructs are needed to work with null.
I have a few specific questions and issues:
- Do I understand correctly that
String?(with a question mark) is a nullable type, andStringis non-null? And if I try to assign null to a variable of typeStringwithout a question mark, will I get a compilation error? - I tried to write simple code, but it doesn't compile:
fun main() {\n val name: No answers yet.
Log in to leave an answer