menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Technology News

>

Integer Ca...
source image

Medium

1M

read

151

img
dot

Image Credit: Medium

Integer Caching in Java Explained: Why 1 == 1 is True but 128 == 128 is False!

  • Primitives (like int) use value comparison when using ==. Objects (like Integer) use reference comparison when using ==.
  • Java maintains an internal cache of frequently used Integer objects in the range -128 to 127 to optimize memory usage.
  • When numbers are within the cache range, Java reuses existing objects; otherwise, new objects are created.
  • It is recommended to use .equals() for value comparison when working with wrapper classes.

Read Full Article

like

9 Likes

For uninterrupted reading, download the app