Method calling in Java - Time & Space Complexity
When we call a method in Java, it takes some time to run. We want to see how this time changes when the method is called many times.
How does the total work grow as we call the method more?
Analyze the time complexity of the following code snippet.
public class Example {
public static void printMessage() {
System.out.println("Hello");
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
printMessage();
}
}
}
This code calls a method that prints a message five times in a loop.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Calling the
printMessage()method inside the loop. - How many times: 5 times, once for each loop iteration.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 5 | 5 method calls |
| 10 | 10 method calls |
| 100 | 100 method calls |
Pattern observation: The number of method calls grows directly with the number of loop iterations.
Time Complexity: O(n)
This means the total time grows in a straight line as we increase the number of method calls.
[X] Wrong: "Calling a method inside a loop is always slow and complex like nested loops."
[OK] Correct: Here, the method is called once per loop iteration, so the time grows simply with the number of calls, not more complex.
Understanding how method calls add up helps you explain code efficiency clearly and shows you can think about how programs grow with input.
"What if the method called inside the loop itself contains a loop? How would the time complexity change?"
Practice
myMethod();Solution
Step 1: Understand what a method call does
Calling a method means running the code inside that method.Step 2: Analyze the example
This runs the instructions saved inside the method namedmyMethod();myMethod.Final Answer:
Runs the instructions inside the method namedmyMethod. -> Option AQuick Check:
Method call = run method code [OK]
- Thinking method call creates or deletes methods
- Confusing method call with printing text
- Assuming method call changes method definition
displayMessage in Java?Solution
Step 1: Recall Java method call syntax
In Java, you call a method by writing its name followed by parentheses.Step 2: Check each option
OnlydisplayMessage();matches the correct syntax for calling a method.Final Answer:
displayMessage(); -> Option DQuick Check:
Method call syntax = name() [OK]
- Omitting parentheses when calling methods
- Adding extra words like 'call' or 'run'
- Using semicolon without parentheses
public class Test {
public static void sayHi() {
System.out.println("Hi there!");
}
public static void main(String[] args) {
sayHi();
}
}Solution
Step 1: Identify the method call in main
Themainmethod callssayHi(), which prints "Hi there!".Step 2: Understand what
It prints the text "Hi there!" to the screen.sayHi()doesFinal Answer:
Hi there! -> Option CQuick Check:
Method prints text = output text [OK]
- Confusing method name with output
- Expecting no output without print statement
- Thinking method call causes error
public class Demo {
public static void greet() {
System.out.println("Hello!");
}
public static void main(String[] args) {
greet;
}
}Solution
Step 1: Check method call syntax in
The code usesmaingreet;which is missing parentheses.Step 2: Understand correct method call
Method calls require parentheses even if no arguments:greet();.Final Answer:
Missing parentheses when calling methodgreet. -> Option BQuick Check:
Method call needs () [OK]
- Forgetting parentheses on method calls
- Thinking semicolon alone calls method
- Assuming method call without () is valid
public class Calculator {
public static int add(int a, int b) {
return a + b;
}
public static void main(String[] args) {
int result = add(5, 3);
System.out.println(result);
}
}What will be printed when the program runs?
Solution
Step 1: Understand method
The methodaddparameters and returnaddtakes two integers and returns their sum.Step 2: Analyze the call
It returns 5 + 3 = 8, which is stored inadd(5, 3)result.Step 3: Check the output statement
System.out.println(result);prints the value 8.Final Answer:
8 -> Option AQuick Check:
Sum of 5 and 3 = 8 [OK]
- Concatenating numbers as strings instead of adding
- Expecting method name printed instead of result
- Confusing return value with method call syntax
Ready to prove it?
Pass the test and this pattern moves unknown
