0
0
Kotlinprogramming~15 mins

Nullable types with ? suffix in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Nullable types with ? suffix
📖 Scenario: Imagine you are creating a simple contact list app. Some contacts have phone numbers, but some do not. You want to store phone numbers in a way that allows missing numbers without causing errors.
🎯 Goal: Learn how to use nullable types with the ? suffix in Kotlin to safely handle values that might be missing.
📋 What You'll Learn
Create a variable with a nullable type using the ? suffix
Assign a value and null to the nullable variable
Use safe call operator ?. to access the nullable variable
Print the results to see how Kotlin handles nullable types
💡 Why This Matters
🌍 Real World
Nullable types help apps handle missing or optional data safely, like missing phone numbers in contacts.
💼 Career
Understanding nullable types is essential for Kotlin developers to avoid crashes and write safe, reliable code.
Progress0 / 4 steps
1
Create a nullable variable
Create a variable called phoneNumber of type String? and assign it the value "123-456-7890".
Kotlin
Need a hint?

Use String? to make the variable nullable.

2
Assign null to the nullable variable
Change the value of phoneNumber to null.
Kotlin
Need a hint?

Just assign null to the variable.

3
Use safe call operator to access nullable variable
Create a variable called length and assign it the length of phoneNumber using the safe call operator ?..
Kotlin
Need a hint?

Use ?. to safely get length without error if phoneNumber is null.

4
Print the length variable
Print the value of length using println.
Kotlin
Need a hint?

Just print length to see the result.