Autoboxed Integer == works up to 127 and breaks at 128

Integer.valueOf caches -128..127, so == compares identical references inside that window and distinct ones outside it. Equality that passes every test with small fixtures fails the first time a real id arrives.

Full code & output

This topic is available to PRO members.

Sign in Create a free account

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-08-25