Default values in Java - Time & Space Complexity
We want to see how the time it takes to run code changes when using default values in Java.
Specifically, we ask: does setting default values affect how long the program runs as input grows?
Analyze the time complexity of the following code snippet.
public class DefaultValues {
public static void fillArray(int[] arr) {
for (int i = 0; i < arr.length; i++) {
arr[i] = 0; // default value
}
}
}
This code fills an array with the default value 0 by looping through each element.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that sets each array element to 0.
- How many times: It runs once for each element in the array, so as many times as the array length.
As the array gets bigger, the number of steps grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 steps |
| 100 | 100 steps |
| 1000 | 1000 steps |
Pattern observation: If you double the array size, the work doubles too.
Time Complexity: O(n)
This means the time to fill the array grows directly with the number of elements.
[X] Wrong: "Setting default values is instant and does not depend on array size."
[OK] Correct: Each element must be set one by one, so the time grows with the array length.
Understanding how loops over arrays affect time helps you explain code efficiency clearly and confidently.
"What if we used a built-in method like Arrays.fill instead of a loop? How would the time complexity change?"
Practice
int variable inside a class?Solution
Step 1: Understand default values for primitive types
In Java, primitive numeric types likeintdefault to 0 if not initialized.Step 2: Apply to
An uninitializedintvariableintinside a class will have the default value 0.Final Answer:
0 -> Option AQuick Check:
int default = 0 [OK]
- Confusing int default with null (which is for objects)
- Thinking boolean default applies to int
- Assuming uninitialized variables cause compile error
Solution
Step 1: Check valid boolean declarations
In Java, a boolean can be declared without initialization; it defaults to false.Step 2: Identify invalid initializations
Assigningnullor0to boolean is invalid and causes errors.Final Answer:
boolean flag; -> Option DQuick Check:
boolean defaults to false if uninitialized [OK]
- Assigning null to boolean (not allowed)
- Using 0 or 1 as boolean values
- Assuming default boolean is true
class Test {
int x;
boolean b;
String s;
void printValues() {
System.out.println(x + "," + b + "," + s);
}
}
public class Main {
public static void main(String[] args) {
Test t = new Test();
t.printValues();
}
}Solution
Step 1: Identify default values for fields
Instance variables: int defaults to 0, boolean to false, and String (object) to null.Step 2: Understand print output
Printing concatenates values with commas, so output is "0,false,null".Final Answer:
0,false,null -> Option AQuick Check:
int=0, boolean=false, String=null [OK]
- Assuming String defaults to empty string instead of null
- Thinking boolean defaults to true
- Expecting compilation error due to uninitialized variables
public class Sample {
static int count;
public static void main(String[] args) {
int number;
System.out.println(count);
System.out.println(number);
}
}What is the error in this code?
Solution
Step 1: Check variable initialization rules
Static variable 'count' is initialized to 0 by default and can be printed.Step 2: Analyze local variable 'number'
Local variables must be explicitly initialized before use; 'number' is not initialized.Final Answer:
Local variable 'number' might not have been initialized -> Option BQuick Check:
Local vars need explicit init [OK]
- Assuming all variables get default values
- Thinking static variables can't be accessed in static methods
- Ignoring compiler error for uninitialized local variables
double values representing temperatures. You declare:double[] temps = new double[5];
What are the default values stored in
temps immediately after this declaration?Solution
Step 1: Understand default values for array elements
In Java, arrays of primitive types get default values for each element; fordouble, it's 0.0.Step 2: Apply to the given array
Afternew double[5], all 5 elements are initialized to 0.0 automatically.Final Answer:
All elements are 0.0 -> Option CQuick Check:
double array defaults to 0.0 [OK]
- Thinking array elements default to null (only for object arrays)
- Assuming elements are NaN by default
- Believing uninitialized array elements cause runtime error
Ready to prove it?
Pass the test and this pattern moves unknown
