0
0
Javaprogramming~5 mins

Procedural vs OOP approach in Java - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Procedural vs OOP approach
O(n)
Understanding Time Complexity

We want to see how the way we organize code affects how long it takes to run.

Does using procedures or objects change how the program grows with bigger input?

Scenario Under Consideration

Analyze the time complexity of the following code snippet.

// Procedural approach
void printNumbers(int n) {
  for (int i = 0; i < n; i++) {
    System.out.println(i);
  }
}

// OOP approach
class NumberPrinter {
  void print(int n) {
    for (int i = 0; i < n; i++) {
      System.out.println(i);
    }
  }
}

Both versions print numbers from 0 up to n-1, but one uses a simple function and the other uses a class method.

Identify Repeating Operations

Look for loops or repeated actions.

  • Primary operation: The for-loop that prints numbers.
  • How many times: Exactly n times, once for each number.
How Execution Grows With Input

The number of print actions grows directly with n.

Input Size (n)Approx. Operations
1010 prints
100100 prints
10001000 prints

Pattern observation: Doubling n doubles the work, showing a straight line growth.

Final Time Complexity

Time Complexity: O(n)

This means the time to run grows directly in step with the input size.

Common Mistake

[X] Wrong: "Using objects always makes the program slower than procedural code."

[OK] Correct: Both approaches here do the same loop, so time depends on the loop, not on using objects or not.

Interview Connect

Understanding how code structure affects time helps you explain your choices clearly and confidently in real projects.

Self-Check

"What if the OOP method called another method inside the loop? How would that affect time complexity?"