Skip to content
Sharp

Core Java · interview question

What does the following code snippet, assuming 'Integer.valueOf' is used evaluate to?

Multiple choiceMediumchapter-7-curatedpremiumcore-javawrapper-types-and-autoboxing
java
Integer a = Integer.valueOf(127);
Integer b = Integer.valueOf(127);
System.out.println(a == b);
Choose one answer
Answer and explanation

Correct answer

B. True

Why

Integer.valueOf() uses cached Integer objects for values between -128 and 127, so '==' checks reference equality and returns true for these values.

Test yourself on Core Java

A free test with instant, explained results. No account needed.

Start free test
All Core Java questions →