100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Common Java Interview Questions

A curated roundup of the most frequently asked core Java interview questions — JVM internals, == vs equals(), String immutability, static vs instance members, and exception handling — with clear, accurate explanations.

Interview PrepIntermediate14 min readJul 7, 2026
Analogies

1. Introduction

This roundup collects the Java interview questions that come up again and again across telephonic screens, onsite loops, and written tests — the kind of questions that check whether you actually understand the language rather than just having memorized syntax.

🏏

Cricket analogy: Like a national selector's trials testing a player across net sessions, practice matches, and fitness tests rather than just a single flashy shot, this roundup covers the Java questions repeated across phone screens, onsites, and written tests.

The goal here is not just to give you an answer to recite, but to explain the reasoning so you can adapt it when an interviewer changes the wording or asks a follow-up 'why' question.

🏏

Cricket analogy: A commentator who explains why a captain set a particular field, not just what the field was, can adapt when the next ball changes everything, the same reasoning-over-recitation goal drives these answers.

2. Syntax

java
String a = "hello";
String b = "hello";
String c = new String("hello");

System.out.println(a == b);       // reference comparison
System.out.println(a == c);       // reference comparison
System.out.println(a.equals(c));  // value comparison
java
final int x = 10;
// x = 20; // compile error: final cannot be reassigned

try {
    System.out.println("try block");
    throw new RuntimeException("boom");
} finally {
    System.out.println("finally always runs");
}

3. Explanation

Interviewers usually build these questions around three themes: (1) memory model and object identity — spot-checked with == vs equals() and String pooling; (2) keyword confusion — static vs instance, final vs finally vs finalize; and (3) control flow guarantees — does finally always execute, what happens with nested try-catch, checked vs unchecked exceptions.

🏏

Cricket analogy: Interviewers probe three areas like a selection panel: player identity, is this the same Virat Kohli or a replacement, mirroring == vs equals; role confusion, captain vs vice-captain duties, mirroring static vs instance; and match protocols, does the innings guarantee always hold, mirroring finally always executing.

A strong answer names the underlying rule first ('== compares references for objects, values for primitives'), then walks through the specific example line by line, and finally states the practical implication ('that's why you should never compare Strings, wrapper objects, or enums with == unless you specifically want reference identity').

🏏

Cricket analogy: A strong answer states the rule first, DRS overturns only marginal umpire's-call decisions, replays the specific ball, then states the implication, just as you should state == compares references before showing why comparing two Strings with == is risky.

Exam strategy: when asked to predict output, first identify whether each variable is a primitive or a reference type. Primitives compare by value everywhere; references compare by identity with == and by contract with .equals(). This single distinction resolves the majority of 'tricky output' questions.

4. Example

java
public class InterviewDemo {
    static int counter = 0;

    int id;

    InterviewDemo() {
        counter++;
        id = counter;
    }

    public static void main(String[] args) {
        Integer a = 127;
        Integer b = 127;
        Integer c = 128;
        Integer d = 128;

        System.out.println(a == b);
        System.out.println(c == d);

        InterviewDemo o1 = new InterviewDemo();
        InterviewDemo o2 = new InterviewDemo();
        System.out.println(o1.id + " " + o2.id + " " + InterviewDemo.counter);
    }
}

5. Output

text
true
false
1 2 2

6. Key Takeaways

  • == compares object references (memory addresses) while .equals() compares logical content; always override equals() (and hashCode()) together for value comparison.
  • Integer caching means autoboxed values from -128 to 127 are reused from a pool, so == happens to return true in that range but not outside it — never rely on this.
  • Strings are immutable in Java; every 'modification' creates a new String object, which is why StringBuilder is preferred for heavy concatenation in loops.
  • static members belong to the class and are shared across all instances, while instance members belong to each object separately.
  • finally executes regardless of whether an exception was thrown, caught, or a return statement was hit in the try/catch — only System.exit() or a JVM crash can skip it.
  • Checked exceptions must be declared or handled at compile time (e.g. IOException); unchecked exceptions (RuntimeException and subclasses) are not enforced by the compiler.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#CommonJavaInterviewQuestions#Common#Interview#Questions#Syntax#StudyNotes#SkillVeris