Safe Call Operator in Kotlin

Level 276 keystrokes

Uses the safe call operator ?. to access members of a nullable String.

Kotlin code

// A nullable String that holds "Hello"
1val text: String? = "Hello"
// ?. reads length only if text is not null (otherwise gives null): prints 5
2println(text?.length)
// uppercase() is called the same way: prints HELLO
3println(text?.uppercase())

Output

5
HELLO
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Kotlin code and how often they appear.

  • ?× 3
  • (× 3
  • )× 3
  • .× 2
  • "× 2
  • :× 1
  • =× 1

More Kotlin examples at level 2

← Kotlin typing practice