Method parameters in Java - Time & Space Complexity
We want to see how the time a method takes changes when we give it different inputs.
How does the method's work grow as the input size changes?
Analyze the time complexity of the following code snippet.
public void printNumbers(int n) {
for (int i = 0; i < n; i++) {
System.out.println(i);
}
}
This method prints numbers from 0 up to n-1, one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that prints numbers.
- How many times: It runs exactly n times, once for each number.
As n grows, the method prints more numbers, so it takes more time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 prints |
| 100 | 100 prints |
| 1000 | 1000 prints |
Pattern observation: The work grows directly with n; doubling n doubles the work.
Time Complexity: O(n)
This means the time grows in a straight line with the input size.
[X] Wrong: "The method always takes the same time no matter the input."
[OK] Correct: The method prints once for each number up to n, so bigger n means more work.
Understanding how method parameters affect time helps you explain your code clearly and think about efficiency.
"What if the method printed numbers only up to n/2? How would the time complexity change?"
Practice
public void greet(String name) { ... }Solution
Step 1: Understand method parameters
Method parameters are variables listed in the method definition that accept values when the method is called.Step 2: Identify their purpose
They allow sending data into the method so it can perform actions using that data.Final Answer:
To send data into methods so they can use it inside -> Option CQuick Check:
Parameters pass data into methods [OK]
- Confusing parameters with return types
- Thinking parameters declare variables inside method body
- Mixing parameters with access modifiers
Solution
Step 1: Recall parameter syntax
Parameters are listed inside parentheses separated by commas, each with a type and name.Step 2: Check each option
public void add(int a, int b) { } uses commas correctly; others use semicolon, missing comma, or invalid symbol.Final Answer:
public void add(int a, int b) { } -> Option BQuick Check:
Parameters separated by commas [OK]
- Using semicolons instead of commas
- Omitting commas between parameters
- Using invalid symbols like &
public class Test {
public static void printSum(int x, int y) {
System.out.println(x + y);
}
public static void main(String[] args) {
printSum(5, 3);
}
}Solution
Step 1: Understand method call with parameters
The method printSum is called with 5 and 3 as arguments, assigned to x and y.Step 2: Calculate the sum inside the method
Inside printSum, it prints x + y which is 5 + 3 = 8.Final Answer:
8 -> Option AQuick Check:
5 + 3 = 8 [OK]
- Concatenating numbers as strings
- Expecting variable names printed
- Thinking code won't compile
public void displayMessage(String message, int) {
System.out.println(message);
}Solution
Step 1: Check parameter list syntax
Each parameter must have a type and a variable name. Here, int has no name.Step 2: Identify the error
Missing variable name after int causes a syntax error.Final Answer:
Parameter list missing variable name for int type -> Option AQuick Check:
Parameters need type and name [OK]
- Omitting parameter names
- Thinking return type is missing
- Misunderstanding print statement usage
Solution
Step 1: Identify correct parameter types and syntax
The method should accept two double parameters named width and height, separated by commas.Step 2: Evaluate each option
public double area(double width, double height) correctly declares two double parameters with names and commas; public double area(width, height) misses types; public double area(int width, int height) uses int instead of double; public void area(double width; double height) uses semicolon instead of comma and void return type.Final Answer:
public double area(double width, double height) -> Option DQuick Check:
Parameters need type, name, comma-separated [OK]
- Omitting parameter types
- Using wrong separators like semicolons
- Using wrong data types for precision
Ready to prove it?
Pass the test and this pattern moves unknown
