Java Integer Cache
Java is not only a language but it’s a technology. Java has well-defined standards. In this article, we will discuss Integer Cache. This feature was introduced in Java 5 in order to improve memory management. Let us first have a look at a sample code to better understand this behavior. Afterward, we’ll discuss why this is implemented.
In the below-given program, you will clearly understand the logic and you will see how you have Initialized variable a and b and how Java keeps holds variable value in integer cache. After that, you will see how instances in the range of -128 to 127. This Integer Cache works only on autoboxing which means Conversion from a primitive type to an object reference is called autoboxing.
Let’s consider an example output of the java code given below:
Here we expected that both should run if condition part. Because we all know that == compare reference and equals() compare data in java. But in this case, the behavior is not as expected. We had seen the output is unexpected.
Java Integer Cache Implementation:
In Java 5, a new feature was introduced to save the memory and improve performance for Integer type objects handling. Integer objects are cached internally and reused via the same referenced objects.
- This is applicable for Integer values in the range between –128 to +127.
- This Integer caching works only on auto-boxing. Integer objects will not be cached when they are built using the constructor.
This is equal to using the valueOf() as follows:
Integer a=10; // this is autoboxing Integer b==new Integer(40); // under the hood
IntegerCache is a private, static, and inner class of Java. As Java maintains standards and this is also nicely documented and gives complete information. Below given is the internal implementation for IntegerCache. Now, you will see the internal implementation logic for IntegerCache in java.
In the below-given program var0,var1,var2,var3, and high for the range to check the values for IntegerCache, and by checking you can also see the range values for IntegerCache such that class is for a cache of values between -128 and 127.
It will help you when you will check any object reference values. And if the value will be in range declaration then the object reference for both values will be the same and if it is not in range declaration then the object reference will be different. So, if you will compare values even if it is same and it might be chance and you have taken out of range values then, in that case, both values even if it will same but will return false means it’s not equal because for both values object reference will be different.
Explanation of above IntegerCache Class :
- The code clearly states that the class is for a cache of values between -128 and 127. The high value of 127 can be modified by using an argument -XX: AutoBoxCacheMax=size.
- It gives you the flexibility to tune the performance according to our application use case.
- It gives you a specific range for choosing numbers from –128 to 127.
- Integer cache gives you the facility to cache other objects also for Example like Byte, Short, Long, etc.