Avatar ·

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:

  1. Do I understand correctly that String? (with a question mark) is a nullable type, and String is non-null? And if I try to assign null to a variable of type String without a question mark, will I get a compilation error?
  2. I tried to write simple code, but it doesn't compile:
fun main() {\n    val name: