ProgrammingHardWipro · Cognizant pattern

What is printed?

What is printed?
public class Main {
    public static void main(String[] args) {
        Integer a = 127;
        int b = 127;
        System.out.print(a == b);
    }
}
  1. A. It depends on the IntegerCache range
  2. B. Compilation error: cannot compare Integer and int with ==
  3. C. false
  4. D. true

Correct answer: D. true

When one operand is a primitive int, == triggers auto-unboxing on the Integer, so this becomes a plain value comparison (127 == 127), always true — the IntegerCache identity rules only matter when BOTH sides are reference types (Integer == Integer).

More OOP with C++ and Java questions

Exams that ask this topic