Back to Predict the Output
Q10Question 10 of 10

What does System.exit() do?

Key Concepts
  • System.exit(0) terminates the JVM immediately with exit code 0 (success)
  • System.exit(1) terminates with exit code 1 (failure) – any non-zero is failure
  • Shutdown hooks (Runtime.addShutdownHook) run before termination
  • finally blocks do NOT run if System.exit() is called inside try
  • Never call System.exit() in web applications or library code – only in standalone CLI apps

Definition

System.exit(int) terminates the JVM immediately. Exit code 0 = success, non-zero = failure. Shutdown hooks run, but finally blocks in the calling thread do not.

System.exit behavior:

  • Terminates the JVM. All threads stop. The process exits.
  • Exit code: 0 = success. Non-zero = failure. Visible to the calling script/OS.
  • Shutdown hooks run. Registered via Runtime.addShutdownHook(Thread). Used for cleanup.
  • finally blocks do NOT run in the thread that calls System.exit().

System.exit is the bank's emergency shutdown. Everyone leaves immediately. The alarm hooks (shutdown hooks) fire, but the teller does not finish their current transaction (finally block).

Understanding the Concepts

How it works

JAVA
try {
    System.out.println("Before exit");
    System.exit(0);       // JVM terminates HERE
    System.out.println("After exit");  // NEVER reached
} finally {
    System.out.println("Finally");  // NEVER reached
}

Output

TEXT
Before exit

Neither "After exit" nor "Finally" is printed.

Shutdown hooks

JAVA
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
    System.out.println("Cleanup: closing connections");
}));

System.exit(0);
// Output:
// Cleanup: closing connections
// (then JVM exits)

When to use System.exit()

  • CLI/batch applications: Exit with 0 on success, 1 on failure.
  • Never in web apps: Calling System.exit() in a servlet/controller terminates the entire server.
  • Never in library code: Let the caller decide when to exit.

What are common mistakes?

A common mistake is assuming the output without tracing through the code step by step.

Wrong approach:

JAVA
package com.bank.quiz;

public class OutputQuiz {
    public static void main(String[] args) {
        // Developers often guess the wrong output
        // because they skip the step-by-step evaluation
        String result = "Balance: " + 100 + 50;
        System.out.println(result);
        // Many guess: "Balance: 150"
        // Actual: "Balance: 10050" (left-to-right string concat)
    }
}

Correct reasoning:

JAVA
package com.bank.quiz;

public class OutputQuiz {
    public static void main(String[] args) {
        // Step 1: "Balance: " + 100 = "Balance: 100" (String + int = String)
        // Step 2: "Balance: 100" + 50 = "Balance: 10050" (String + int = String)
        String result = "Balance: " + 100 + 50;
        System.out.println(result);  // Balance: 10050

        // To get arithmetic: use parentheses
        String correct = "Balance: " + (100 + 50);
        System.out.println(correct);  // Balance: 150
    }
}

Always trace left to right and remember that once a String is involved in concatenation, everything after it becomes string concatenation too.

What should a senior engineer know?

A senior engineer working on banking systems should understand the production implications of this concept.

  • Performance impact. Measure with JMH before making assumptions about which approach is faster.
  • Thread safety. In a multi-threaded banking service processing concurrent transactions, always consider whether the approach is thread-safe.
  • Memory footprint. Use JFR and VisualVM to measure actual memory usage under realistic banking workloads.
  • Monitoring. Set up JMX metrics and alerts for production banking applications.
JAVA
package com.bank.monitoring;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;

public class MemoryMonitor {
    public static void printMemoryUsage() {
        MemoryMXBean mem = ManagementFactory.getMemoryMXBean();
        System.out.println("Heap: " + mem.getHeapMemoryUsage());
        System.out.println("Non-Heap: " + mem.getNonHeapMemoryUsage());
    }
}

What are the edge cases?

Edge cases can cause unexpected behavior in banking applications. Here are some to watch for.

JAVA
package com.bank.edge;

public class EdgeCaseDemo {
    public static void main(String[] args) {
        // Edge case 1: null handling
        String accountId = null;
        try {
            System.out.println(accountId.length());
        } catch (NullPointerException e) {
            System.out.println("Null account ID");
        }

        // Edge case 2: empty vs null
        String emptyId = "";
        System.out.println(emptyId.isEmpty());  // true
        System.out.println(emptyId == null);     // false

        // Edge case 3: boundary values
        java.math.BigDecimal zero = java.math.BigDecimal.ZERO;
        java.math.BigDecimal negativeBalance = new java.math.BigDecimal("-0.01");
        System.out.println(zero.compareTo(negativeBalance) > 0);  // true
    }
}
TEXT
Null account ID
true
false
true

Always test with null values, empty strings, boundary values, and negative amounts in banking applications.

What are common interview follow-up questions?

Interviewers often follow up with these related questions:

  • How does this concept apply to a high-throughput banking transaction system?
  • What are the performance implications in a production environment?
  • How would you debug issues related to this concept?
  • What tools would you use to monitor this in production?
  • How does this behave differently in Java 8 vs Java 17+?

When answering follow-up questions, always relate your answer back to practical experience. For example, at National Bank of India, Rajesh debugged a production issue related to this concept by using JFR recordings and heap dumps. Providing concrete examples from banking systems shows the interviewer that you understand both the theory and the practice.

Visual Overview

DIAGRAM
  System.exit(0):
    → shutdown hooks run
    → finally blocks do NOT run
    → JVM terminates
    → exit code 0 returned to OS

  System.exit(1):
    → same, but exit code 1 (failure)

  Rule: only in CLI apps, never in web apps or libraries

Key Takeaway

System.exit() terminates the JVM immediately. Shutdown hooks run but finally blocks do not. Use exit code 0 for success, non-zero for failure. Never call it in web applications or library code.

One sentence to remember: System.exit() kills the JVM – shutdown hooks run, finally blocks do not.

---

Share
Use ← → arrow keys to navigate