kotlin-by-example
kotlin-by-example copied to clipboard
The documentation for Null Safety doesn't work this way.
Null Safety In an effort to rid the world of NullPointerException, variable types in Kotlin don't allow the assignment of null. If you need a variable that can be null, declare it nullable by adding ? at the end of its type.
fun main() { //sampleStart var neverNull: String = "This can't be null" // 1
neverNull = null // 2
var nullable: String? = "You can keep a null here" // 3
nullable = null // 4
var inferredNonNull = "The compiler assumes non-null" // 5
inferredNonNull = null // 6
fun strLength(notNull: String): Int { // 7
return notNull.length
}
It should be this in documentation : println(strLength(neverNull)); // 8 println(strLength(nullable)); // 9
instead of this: strLength(neverNull) // 8 strLength(nullable) // 9 //sampleEnd }
i have already created the pull request for this.