Skip to content

Quirk: Integer cache and == on boxed numbers #88

Open

Description

@mperor

Goal: explain why Integer a = 127, b = 127; a == b is true but fails for 128.

Show:

  • the -128..127 cache from Integer.valueOf (autoboxing) vs new Integer (deprecated)
  • same behaviour for Long, Short, Byte, Character (0..127); none for Double/Float
  • the fix: equals / unboxing; NPE when unboxing null

Where: new package FirstSteps/src/test/java/pl/mperor/lab/java/quirk

Activity

  1. added
    track:quirksSurprising behaviour and classic pitfalls
    dailyQueued for the daily learning PR
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    dailyQueued for the daily learning PRtrack:quirksSurprising behaviour and classic pitfalls

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions