JAVA Integer caching

Dinuka Kasun Medis
3 min readJust now

--

Generated with Gemini AI

Hi in this article I am going to keep a note about “Integer caching in JAVA”. So let’s go to the note.

Let’s visualize the process of `Integer` object creation for `a = 128` and `b = 128`.

1. Integer Caching (-128 to 127)

For integers in the range `-128` to `127`, Java uses a cache. If you assign a value within this range to an `Integer`, Java will not create a new object. Instead, it reuses an existing cached object.

For example:

Integer x = 127;
Integer y = 127;

/**
Both `x` and `y` point to the **same object** in memory:

+ - - - - -+
| 127 | ← x
+ - - - - -+
^
|
y

**/

2. For Integer a = 128; and Integer b = 128;

Since `128` is outside the cache range, Java will create two separate `Integer` objects:

Integer a = 128;
Integer b = 128;

/**
This results in the following memory layout:
+ - - - - -+ + - - - - -+
| 128 | | 128 |
+ - - - - -+ + - - - - -+
^ ^
| |
a b

**/

// Here, `a` and `b` point to **different objects**, so:

System.out.println(a == b); // false

This diagram illustrates that while the value `128` is the same, the objects in memory are different.

Java uses a cache for `Integer` objects in the range of `-128` to `127` to optimize memory usage and improve performance. This behavior is a part of the **Java Integer Cache** mechanism.

Reasons for Using Integer Caching

1. Memory Efficiency

- Integers in the range of `-128` to `127` are very commonly used values in many programs (e.g., for loops, counters, small calculations). Instead of creating new `Integer` objects every time these values are used, Java reuses already-created objects.

- This reduces the number of objects created in memory, thus lowering memory overhead.

2. Performance Boost

- Object creation and garbage collection can be costly in terms of time and computational resources. By caching commonly used integer objects, Java avoids the overhead of creating new objects, which improves the performance of the application.

3. Immutable Nature of Integer

- Since `Integer` objects are **immutable** (i.e., their value cannot change once created), it’s safe to share the same object for multiple references. This immutability allows the same `Integer` object to be reused without the risk of accidental modification.

4. Range Selection (-128 to 127)

- The specific range of `-128` to `127` was chosen because these values fit within a **byte** (which is 8 bits, ranging from `-128` to `127`), making it a compact and frequently used range. These are the most common small numbers that programs need.

Example:

If you create multiple `Integer` objects within the cached range, Java will reuse the same object:

Integer x = 100;
Integer y = 100;

System.out.println(x == y); // true

No new object is created for `y` — it simply references the cached object that `x` is also pointing to.

Outside the Range (-128 to 127):

For values **outside** this range, like `128`, a new `Integer` object is created every time, unless explicitly cached or managed in some other way.

Integer a = 128;
Integer b = 128;

System.out.println(a == b); // false

Each assignment creates a new object in memory, which is why `a` and `b` are different.

Integer Cache Customization:

As of Java 5, you can actually modify the size of this cache by setting the JVM option `-XX:AutoBoxCacheMax=<size>`. By default, it is `127`, but it can be expanded if needed.

Example:

java -XX:AutoBoxCacheMax=1000 MyClass

This would expand the cache to include values up to `1000` instead of the default range of `-128` to `127`.

--

--

Dinuka Kasun Medis

Hej, jag heter Dinuka Kasun. Välkommen till min profil. Arbeta som mjukvaruingenjör. Och nu för tiden lär jag mig svenska. 🤓🇱🇰 🇸🇪