kotlin-by-example icon indicating copy to clipboard operation
kotlin-by-example copied to clipboard

The documentation for Null Safety doesn't work this way.

Open Nirajan1-droid opened this issue 1 year ago • 2 comments

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.

Nirajan1-droid avatar Feb 23 '24 10:02 Nirajan1-droid