Bird
0
0

Consider this code:

hard📝 Application Q9 of 15
Java - Wrapper Classes
Consider this code:
Integer a = 127;
Integer b = 127;
Integer c = 128;
Integer d = 128;
System.out.println(a == b);
System.out.println(c == d);

What is the output and why?
Afalse false because both are different objects
Btrue false because 127 is cached but 128 is not
Ctrue true because all values are autoboxed
Dfalse true because 128 is cached but 127 is not
Step-by-Step Solution
Solution:
  1. Step 1: Recall Integer cache range

    Java caches Integer objects for values from -128 to 127, so a and b reference the same object.
  2. Step 2: Analyze comparison results

    c and d are outside cache range, so they are different objects; == returns false.
  3. Final Answer:

    true false because 127 is cached but 128 is not -> Option B
  4. Quick Check:

    Integer cache affects == comparison results [OK]
Quick Trick: Integer cache range is -128 to 127 for == comparisons [OK]
Common Mistakes:
  • Assuming == compares values always
  • Ignoring cache range effect
  • Confusing autoboxing with caching

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes