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?"
Practice
String to a primitive int in Java?Solution
Step 1: Understand method purpose
parseIntconverts a string directly to a primitiveint.Step 2: Differentiate from other methods
valueOfreturns anIntegerobject, not a primitive.toStringconverts int to string.intValue()is called on an Integer object to get primitive int.Final Answer:
Integer.parseInt(String s) -> Option CQuick Check:
parseInt returns int [OK]
- Confusing parseInt and valueOf
- Using intValue() without an Integer object
- Thinking toString converts string to int
int variable num to an Integer object?Solution
Step 1: Identify method to get Integer object
valueOfconverts primitiveinttoIntegerobject.Step 2: Check other options
parseIntreturns primitive int, not Integer object.intValue()is a method of Integer, not int.toStringreturns String, not Integer.Final Answer:
Integer.valueOf(num) -> Option AQuick Check:
valueOf returns Integer object [OK]
- Using parseInt with int input
- Calling intValue() on primitive int
- Confusing toString with valueOf
String s = "123"; Integer obj = Integer.valueOf(s); int n = obj.intValue(); System.out.println(n + 10);
Solution
Step 1: Convert String to Integer object
Integer.valueOf("123")creates Integer object with value 123.Step 2: Extract primitive int and add 10
intValue()returns 123 as int. Adding 10 results in 133.Final Answer:
133 -> Option AQuick Check:
123 + 10 = 133 [OK]
- Thinking output is string concatenation
- Expecting NumberFormatException for valid number string
- Confusing intValue() usage
String s = "456"; int n = Integer.valueOf(s); System.out.println(n);
Solution
Step 1: Check return type of Integer.valueOf
valueOfreturns anIntegerobject.Step 2: Auto-unboxing allows assignment
Java (5+) automatically unboxesIntegertoint, so the code compiles and runs, printing 456.Final Answer:
No error, code runs fine -> Option DQuick Check:
valueOf(String) auto-unboxes to int [OK]
- Forgetting auto-unboxing feature
- Assuming valueOf returns primitive int
- Thinking String can't convert to Integer
["10", "20", "30", "40"]. Which code snippet correctly converts this list to a list of Integer objects using wrapper methods?Solution
Step 1: Understand method return types
Integer.valueOf(String)returns an Integer object, suitable for list of Integer.Step 2: Check other options
String::parseIntinvalid - no parseInt on String.s.intValue()not a method on String.toStringreturns String, not Integer.Final Answer:
list.stream().map(Integer::valueOf).collect(Collectors.toList()) -> Option BQuick Check:
valueOf converts String to Integer object [OK]
- Thinking parseInt is a String method
- Calling intValue() on String
- Using toString instead of valueOf
Ready to prove it?
Pass the test and this pattern moves unknown
