0
0
Kotlinprogramming~15 mins

Non-nullable types by default in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
Non-nullable types by default
📖 Scenario: You are creating a simple Kotlin program to store and display a person's name and age. Kotlin treats types as non-nullable by default, which means variables cannot hold null unless explicitly allowed.
🎯 Goal: Build a Kotlin program that defines a non-nullable name variable and a non-nullable age variable, then prints them.
📋 What You'll Learn
Create a non-nullable name variable of type String with the value "Alice".
Create a non-nullable age variable of type Int with the value 30.
Print the name and age variables in the format: Name: Alice, Age: 30.
💡 Why This Matters
🌍 Real World
Non-nullable types help prevent errors by ensuring variables always have valid values, which is important in apps that handle user data.
💼 Career
Understanding Kotlin's type system is essential for Android developers and anyone working with Kotlin to write safe and reliable code.
Progress0 / 4 steps
1
Create non-nullable variables
Create a non-nullable variable called name of type String and set it to "Alice".
Kotlin
Need a hint?

Use val to declare a read-only variable and specify the type String without a question mark.

2
Add a non-nullable age variable
Add a non-nullable variable called age of type Int and set it to 30 below the name variable.
Kotlin
Need a hint?

Remember to declare age as Int without a question mark to keep it non-nullable.

3
Create a message string
Create a variable called message of type String that uses an f-string (string template) to combine name and age in the format: Name: Alice, Age: 30.
Kotlin
Need a hint?

Use Kotlin string templates with $variableName inside double quotes.

4
Print the message
Print the message variable using println.
Kotlin
Need a hint?

Use println(message) to display the message on the screen.