Runtime class — Java Technical Interview Questions & Answers

Runtime class

Runtime class

  • Garbage collection frees heap objects that nothing points to anymore. You don’t call free() like C++. The JVM decides when to collect.
  • You can hint with System.gc() but you cannot force it. Unreachable objects become eligible. Memory leaks still happen if you keep unwanted references (static lists, caches).
  • Don’t say “Java has no memory problems”. GC helps, it does not make leaks impossible.
  • Let's take this on the board with one tiny Main class — no extra files. Output: Asha scored 72 then Pass. Real Main. Swap marks/name to show one idea from Runtime class. Start from main, go line by line, and stop at each print. That output is the proof for Runtime class.

Technical Interview Tip: Explain Runtime class as: definition → why → one code idea.

Code Examples for Runtime class

Example

// Runtime
public class Main {
  public static void main(String[] args) {
    int marks = 72;
    String name = "Asha";
    System.out.println(name + " scored " + marks);
    if (marks >= 40) System.out.println("Pass");
  }
}

Output: Asha scored 72 then Pass. Real Main. Swap marks/name to show one idea from Runtime class.

How to Explain Runtime class to an Interviewer

What is Runtime class?

GC frees heap objects that nothing points to. You don’t free() yourself. JVM decides when. System.gc() is only a hint, not a force.

Why do we use Runtime class in Java?

Makes the program clearer or safer GC frees heap objects that nothing points to.

Write one small example of Runtime class.

Show syntax Say what prints

What mistake do beginners make with Runtime class?

Only the name, no example Keeping unwanted references → leak even with GC.

Where is Runtime class used in a real program?

Day-to-day Java code