Common wrapper methods in Java - Time & Space Complexity
We want to see how fast common wrapper methods run as the input size grows.
How does the time to run these methods change when we use bigger or smaller data?
Analyze the time complexity of these wrapper method calls.
Integer num = Integer.valueOf(100);
String str = num.toString();
int val = num.intValue();
boolean isEqual = num.equals(100);
This code uses common wrapper methods to convert, compare, and get values.
Look for loops or repeated work inside these methods.
- Primary operation: Simple method calls without loops.
- How many times: Each method runs once per call.
These methods do a fixed amount of work regardless of input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 1 operation |
| 100 | About 1 operation |
| 1000 | About 1 operation |
Pattern observation: The time stays the same no matter how big the input is.
Time Complexity: O(1)
This means the methods take the same short time no matter the input size.
[X] Wrong: "Calling toString() or equals() takes longer if the number is bigger."
[OK] Correct: These methods do simple fixed steps, so size or value does not affect their speed.
Knowing these methods run quickly helps you explain your code's efficiency clearly and confidently.
"What if we called toString() on a very large collection instead of a single number? How would the time complexity change?"
