Return values in Java - Time & Space Complexity
We want to see how the time to get a return value changes as input grows.
How does the program's work increase when returning values?
Analyze the time complexity of the following code snippet.
public int getFirstElement(int[] arr) {
return arr[0];
}
This code returns the first item from an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Accessing a single element by index.
- How many times: Exactly once, no loops or repeats.
Getting one element takes the same time no matter how big the array is.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 |
| 100 | 1 |
| 1000 | 1 |
Pattern observation: The work stays the same even if the input grows.
Time Complexity: O(1)
This means the time to return a value does not change with input size.
[X] Wrong: "Returning a value from an array takes longer if the array is bigger."
[OK] Correct: Accessing an element by index is direct and does not depend on array size.
Understanding how return values work helps you explain code efficiency clearly and confidently.
"What if we changed the method to search for a value instead of returning by index? How would the time complexity change?"
Practice
return keyword do in a Java method?Solution
Step 1: Understand the purpose of
Thereturnreturnkeyword is used inside a method to send a value back to the caller.Step 2: Differentiate from other actions
Stopping the program, printing, or declaring variables are different actions and not whatreturndoes.Final Answer:
It sends a value back from the method to where it was called. -> Option BQuick Check:
Return sends value back [OK]
- Thinking return stops the whole program
- Confusing return with print statements
- Assuming return declares variables
Solution
Step 1: Check method return type and return statement
The method must declareintas return type to return an integer value.Step 2: Match return type with returned value
public int getNumber() { return 5; } declaresintand returns 5, which is an integer, so it is correct.Final Answer:
public int getNumber() { return 5; } -> Option CQuick Check:
Return type matches returned value [OK]
- Using void but returning a value
- Returning wrong type value
- Missing return statement in non-void method
public class Test {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
System.out.println(add(3, 4));
}
}Solution
Step 1: Understand the add method
The method adds two integers and returns their sum.Step 2: Calculate add(3, 4)
3 + 4 equals 7, so the method returns 7.Final Answer:
7 -> Option AQuick Check:
3 + 4 = 7 [OK]
- Concatenating numbers as strings
- Expecting error due to return
- Confusing return with print
public int multiply(int x, int y) {
int result = x * y;
}Solution
Step 1: Check method return type
The method declares it returns an int, so it must return an int value.Step 2: Verify return statement presence
The method calculates result but does not return it, so it misses a return statement.Final Answer:
Method is missing a return statement. -> Option DQuick Check:
Non-void methods must return a value [OK]
- Forgetting to return value in non-void method
- Changing return type to void without reason
- Assuming variable declaration is error
public static String checkNumber(int n) {
if (n > 0) return "Positive";
else if (n < 0) return "Negative";
else return "Zero";
}What will
checkNumber(0) return?Solution
Step 1: Analyze the if-else conditions
If n > 0 returns "Positive", if n < 0 returns "Negative".Step 2: Check the else case for n = 0
Since 0 is neither greater nor less than 0, else returns "Zero".Final Answer:
"Zero" -> Option AQuick Check:
0 triggers else return "Zero" [OK]
- Assuming 0 is positive or negative
- Missing else return case
- Expecting null when no return
Ready to prove it?
Pass the test and this pattern moves unknown
