Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Nirajan1-droid opened this issue Feb 23, 2024 · 2 comments
Open

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

Nirajan1-droid opened this issue Feb 23, 2024 · 2 comments

Comments

@Nirajan1-droid
Copy link

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.

@Ahmadpansota
Copy link

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
}

@Ahmadpansota
Copy link

3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants