Everything About Java Integer Caching

Ankit Khatri
5 min readSep 30, 2021

--

In this article, you will have a really good understanding of java integer caching.
In this article, I’m going to explain:
1) What is Java Integer Caching?

2) What is the need for Java Integer Caching?

3) How does the caching internally work?

4) Differences between int and Integer.

5) Some examples based on Java Integer Caching.

Before going to deep dive into the article, You must understand that Java Integer Caching is only for java.lang.Integer class not for “int” primitive datatype.

So, let’s deep dive into it.
“Java Integer Caching” is a technique for caching the integer. If you don’t know what is caching, let me explain you with an example. When we need to perform the same task very frequently, we use some extra space (cache memory) to store the elements/values which occurs very frequently. Next time when we want to again look for the values, we first check in the cache memory, if it’s available, we simply say yes this value is present. If it's not present in the cache, then we will look for the values in the “entire memory”. So where does caching help if we require to look for the element in the entire memory.! Caching saves a lot of time here since I have mentioned that the values are being searched very “frequently”. So, if you search for the same value, again and again, you don't need to recheck the entire memory again and again.
Similarly, In Java, we had to make Integer very faster datatype than long, float, double, etc because “Integer” is the most used data type in any type of project. So Integer must be cached in order to boost up its performance.

Since caching is all about allocating some extra space for boosting performance. So we can allocate a huge cache area to more boost its performance, but the cache memory is really costly, and allocating huge space for caching will require a lot of management and cost. So we are limited with the cache space and the cost as well.
So, Java developers created cache memory of range(-128, to 127). This means any number that lies in this range(including -128 and 127) will be cached. That’s because we use more often “Integer a = 10, Integer b = 0, Integer c = 1, Integer d = -1” something like in this range rather than “Integer x = 100000, Integer y = -487478”. So, when you assign an integer value, if the integer value is in range -128 to 127, it will create an object in “cache area” (inside heap area, some extra memory is allocated named as cache) and the rest of the integer values will be created in “heap area”. If you try to create another object of the same value which is already present in “cache memory”, it will not create another object in cache memory and will assign the reference of the same object. Thus, two references are pointing to the same object which is not in the case of other integer values except the above range. Let's look at this picture for a very insightful view.

Internal Working structure of Java Integer Caching

Now, you have got an idea about the internal working architecture of Java Integer Caching. Let's move to the next section.

Differences between int and Integer in Java

There are many differences between these two, I’m only going to explain what’s important here. You can check out more differences on the internet.

  1. int is a primitive data type that means no object is created. It gets created inside the stack area and the value is also assigned to the stack area at the compile time. Whereas Integer is a class in java, so Integer a = 100, here a is an object of type java.lang.integer class and an integer object with value 100 will be created in the heap area at runtime.
  2. == operator is used for reference comparison only if you are using two objects. Let's say “Integer a = 10, Integer b = 20;” and (a == b) will compare the address (references) of object a and object b not the value of object and object b. Whereas if “int x = 10, int y = 20” and (x==y) will compare the content/value which variable is holding. So we conclude that == operator is used for reference comparison only if we compare two objects, if we compare two primitive datatype values, it will compare the actual value.

Some examples are widely used in interviews to test the candidate’s knowledge of java.

// Write the output for the following sample code.

  1. Integer A= 1000, B= 1000;
  2. int X= 1000, Y = 1000;
  3. System.out.println(A == B);
  4. System.out.println(X == Y);

==> If your answer is true(for line 3) & true(for line 4), then you’re wrong. The correct answer is false(for line 3) & true(for line 4).
Explanation:

Integer is a class, so a and b are the objects of integer type and these values are not in the range of (-128 to 127) so two new objects will be created for A as well as B, since == operator is for reference comparison when we compare two objects, so here the two different memory location is compared and both the memory location is different that's why it provides false. Whereas using == operator for int which is a primitive datatype, will compare the actual content/value i.e 1000 and 1000 which is the same, so it will provide true as the answer.

// write the output for given sample code

  1. Integer A = 100, B = 100;
  2. int X = 100, Y = 100;
  3. System.out.println(A == B);
  4. System.out.println(X == Y);

==> Now, if you have got the right answer here, I believe you have clearly understand the concept of Java Integer Caching. So the correct answer is true (for line 3) and true (for line 4)

Explanation:

In line 3, Two integer objects are compared with == operator so, its reference comparison. Since value of A and value of B are in the range of (-128 to 127), so only one object will be created and B will get the same object references what A does have as mentioned in the above picture diagram. So both the object are pointing to the same location, so it will provide the result as true and X and Y are primitive datatypes and == operator will content comparison if there is primitive datatype. So x is holding 100 and y is also holding 100. so the answer will be true here.

Thanks, That’s all about Java Integer Caching. I hope you enjoyed the article. Please give a thumbs up. Thank you.

--

--