Non-nullable types by default in Kotlin - Time & Space Complexity
We want to see how the program's steps grow when using non-nullable types in Kotlin.
How does Kotlin handle checking for null values as the program runs?
Analyze the time complexity of the following code snippet.
fun printLength(text: String) {
println(text.length)
}
fun main() {
val input: String = "Hello, Kotlin!"
printLength(input)
}
This code prints the length of a non-nullable string passed to a function.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing the length property of a non-nullable string.
- How many times: Exactly once per function call.
Getting the length of a string takes the same amount of time no matter how long the string is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The operation count stays the same even if the string is longer.
Time Complexity: O(1)
This means the time to get the string length does not grow with input size.
[X] Wrong: "Checking for null in Kotlin slows down the program as the input grows."
[OK] Correct: Kotlin's non-nullable types are checked at compile time, so no extra work happens at runtime as input size grows.
Understanding how Kotlin handles null safety helps you write safer code without worrying about slowing down your program.
"What if the function accepted a nullable String instead? How would the time complexity change?"
Practice
? after its type?Solution
Step 1: Understand Kotlin's default type behavior
Kotlin types are non-nullable by default, meaning variables cannot holdnullunless specified.Step 2: Check the effect of missing
Without??, the variable type is non-nullable, so it cannot holdnull.Final Answer:
The variable cannot holdnullvalues. -> Option AQuick Check:
Non-nullable type = nonullallowed [OK]
? means no null allowed [OK]- Thinking variables are nullable by default
- Assuming variables initialize to null automatically
- Confusing
lateinitwith nullability
null values?Solution
Step 1: Identify nullable type syntax
Adding?after the type allows the variable to holdnull.Step 2: Check initialization
Nullable variables must be initialized or declared withnullexplicitly, soval name: String? = nullis correct.Final Answer:
val name: String? = null -> Option AQuick Check:
Nullable type with?and initialized tonull[OK]
? and initialize nullable variables [OK]- Declaring nullable without
? - Not initializing nullable variables
- Assigning
nullto non-nullable types
fun main() {
val text: String? = null
println(text?.length ?: "No text")
}Solution
Step 1: Understand safe call operator usage
Thetext?.lengthsafely accesseslengthonly iftextis notnull.Step 2: Evaluate the Elvis operator
Sincetextisnull,text?.lengthisnull, so the Elvis operator?:returns "No text".Final Answer:
No text -> Option DQuick Check:
Safe call + Elvis operator returns fallback [OK]
- Expecting 0 instead of fallback string
- Confusing safe call with normal call
- Thinking code causes error due to null
fun main() {
val number: Int = null
println(number)
}Solution
Step 1: Check variable type and assignment
The variable 'number' is declared as non-nullable Int but assignednull, which is not allowed.Step 2: Understand Kotlin null safety rules
Non-nullable types cannot holdnull, so this causes a compilation error.Final Answer:
Cannot assign null to a non-nullable type Int. -> Option BQuick Check:
Non-nullable types rejectnullassignments [OK]
- Thinking semicolon is required
- Assuming var fixes null assignment
- Ignoring Kotlin's null safety
val input: String?. You want to print its length only if it is not null, otherwise print "Empty input". Which Kotlin code correctly does this?Solution
Step 1: Understand nullable variable usage
Sinceinputcan benull, we must check before accessinglength.Step 2: Evaluate each option
println(input.length ?: "Empty input") is invalid becauselengthis Int, not nullable, so Elvis operator on it is wrong.
println(if (input != null) input.length else "Empty input") safely checksnulland prints length or fallback.
println(input!!.length) uses!!which throws exception ifnull.
println(input?.length) printsnullifinputisnull, not the string "Empty input".Final Answer:
println(if (input != null) input.length else "Empty input") -> Option CQuick Check:
Explicit null check with if-else for safe access [OK]
- Using !! and risking exceptions
- Misusing Elvis operator on non-nullable Int
- Printing null instead of fallback string
Ready to prove it?
Pass the test and this pattern moves unknown
