0
0
Kotlinprogramming~10 mins

Non-nullable types by default in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Non-nullable types by default
Declare variable
Type is non-nullable by default
Assign non-null value
YesWorks fine
No
Compile error: Null not allowed
Use nullable type with ?
Can assign null or non-null value
Kotlin variables are non-nullable by default, so you cannot assign null unless you explicitly allow it with a nullable type.
Execution Sample
Kotlin
var name: String = "Alice"
// name = null // Error: Null not allowed
var nullableName: String? = "Bob"
nullableName = null
This code shows a non-nullable String variable and a nullable String? variable, demonstrating assignment rules.
Execution Table
StepVariableTypeAssigned ValueAllowed?Notes
1nameString"Alice"YesNon-nullable variable assigned non-null value
2nameStringnullNoCompile error: cannot assign null to non-nullable
3nullableNameString?"Bob"YesNullable variable assigned non-null value
4nullableNameString?nullYesNullable variable assigned null value
💡 Execution stops because assigning null to non-nullable variable causes compile error
Variable Tracker
VariableStartAfter 1After 2After 3After 4
nameuninitialized"Alice"error (null assign)error (null assign)error (null assign)
nullableNameuninitializeduninitializeduninitialized"Bob"null
Key Moments - 2 Insights
Why does assigning null to 'name' cause an error?
Because 'name' is declared as a non-nullable String (see execution_table step 2), Kotlin does not allow null assignment to prevent null pointer errors.
How can a variable hold null in Kotlin?
By declaring the variable type with a question mark (String?), it becomes nullable and can hold null values (see execution_table steps 3 and 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what happens when we assign null to 'name' at step 2?
AIt works and 'name' becomes null
BCompile error occurs
C'name' becomes empty string
DProgram crashes at runtime
💡 Hint
Check execution_table row for step 2 under 'Allowed?' and 'Notes'
At which step does 'nullableName' first hold a null value?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Look at execution_table rows for 'nullableName' and see when 'Assigned Value' is null
If we remove the question mark from 'nullableName' type, what changes in the execution table?
A'nullableName' can still hold null without error
B'nullableName' becomes an integer
CAssigning null to 'nullableName' would cause a compile error
DNo change in behavior
💡 Hint
Refer to key_moments about nullable types and null assignment
Concept Snapshot
Kotlin variables are non-nullable by default.
You cannot assign null to a non-nullable type.
Use '?' after type to allow null (nullable type).
Nullable variables can hold null or non-null values.
This helps avoid null pointer errors at runtime.
Full Transcript
In Kotlin, variables are non-nullable by default, meaning you cannot assign null to them. For example, a variable declared as 'var name: String' must always hold a non-null string. If you try to assign null to it, the compiler will show an error and stop the program from compiling. To allow a variable to hold null, you must declare it as nullable by adding a question mark after the type, like 'var nullableName: String?'. This variable can then hold either a string or null safely. This design helps prevent null pointer exceptions by catching null assignments at compile time.