Skip to content
Sharp

Core Java · interview question

Why does using Integer.valueOf(int) over new Integer(int) matter in practice?

Multiple choiceHardchapter-7-curatedpremiumcore-javawrapper-types-and-autoboxing
Choose one answer
Answer and explanation

Correct answer

A. Better performance due to caching

Why

Integer.valueOf(int) uses caching for values between -128 and 127, which means it can reuse existing objects, improving performance and reducing memory usage compared to new Integer(int) which always creates a new object.

Test yourself on Core Java

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

Start free test
All Core Java questions →