What if your app never crashed because of missing or changed data? Kotlin's variables and null safety make that possible!
Why Variables (val, var) and null safety in Android Kotlin? - Purpose & Use Cases
Imagine writing an app where you keep track of user info like names and ages. You write code to store these details, but sometimes you forget if a value can change or if it might be missing. This causes bugs and crashes.
Without clear rules, your app might crash when it tries to use a missing value (null). Also, if you accidentally change a value that should stay the same, your app behaves unpredictably. Fixing these bugs takes a lot of time and makes your code messy.
Kotlin's val and var keywords clearly show if a value can change or not. Plus, Kotlin's null safety forces you to handle missing values carefully, preventing crashes before they happen.
String name = null; name = "Alice"; name = null; // Oops, no warning String age; // No info if it can change
val name: String? = null // Nullable, must check before use
var age: Int = 0 // Mutable, can change safelyThis lets you write safer, clearer apps that don't crash unexpectedly and are easier to understand and maintain.
In a messaging app, you can safely handle when a user hasn't set a profile picture yet (null) without the app crashing, and you know which info can be updated or stays fixed.
val means a value won't change, var means it can.
Null safety helps avoid crashes by forcing you to handle missing values.
Using these makes your app more reliable and your code easier to read.
Practice
Solution
Step 1: Understand Kotlin variable keywords
valis used for read-only variables, meaning their value cannot change once assigned.Step 2: Compare with other keywords
varallows changing the value;constis not a Kotlin keyword for variables;letis from other languages like Swift or JavaScript.Final Answer:
val -> Option CQuick Check:
Immutable variable = val [OK]
- Confusing val and var
- Using const which is not Kotlin variable keyword
- Thinking let is Kotlin keyword
Solution
Step 1: Understand nullable type syntax
In Kotlin, adding?after the type means the variable can hold null.Step 2: Check initialization
Nullable variables should be initialized withnullif no value is assigned yet.Final Answer:
var name: String? = null -> Option AQuick Check:
Nullable variable syntax = var name: String? = null [OK]
- Assigning null to non-nullable type
- Forgetting ? for nullable types
- Using string "null" instead of null
var count: Int? = 5 count = null println(count)
Solution
Step 1: Analyze variable declaration
countis declared as nullable Int (Int?) and initially set to 5.Step 2: Assign null and print
Thencountis assignednull, which is allowed for nullable types, so printing it outputsnull.Final Answer:
null -> Option AQuick Check:
Nullable variable assigned null prints null [OK]
- Expecting 5 after reassignment
- Thinking null prints 0
- Assuming compilation error for null assignment
val message: String = null println(message)
Solution
Step 1: Check variable type and assignment
messageis declared as non-nullableStringbut assignednull, which is not allowed.Step 2: Understand Kotlin null safety rules
Non-nullable types cannot hold null; to allow null, type must beString?.Final Answer:
Cannot assign null to non-nullable type String -> Option DQuick Check:
Non-nullable String cannot be null [OK]
- Thinking val disallows null but var allows
- Assuming println can't print val
- Ignoring Kotlin's null safety rules
Solution
Step 1: Understand mutability and nullability
The variable must be changeable, sovaris needed, and it must allow null, so type must be nullable with?.Step 2: Check each option
val data: Int = null usesval(immutable) and assigns null to non-nullable type (error). var data: Int? = null usesvarand nullable type with null initial value (correct). val data: Int? = 0 usesval(immutable) but nullable type with 0 initial value (not changeable). var data: Int = null usesvarbut non-nullable type assigned null (error).Final Answer:
var data: Int? = null -> Option BQuick Check:
Mutable and nullable = var with ? [OK]
- Using val when variable should change
- Assigning null to non-nullable type
- Confusing val and var usage
Ready to prove it?
Pass the test and this pattern moves unknown
