Java caches Integer objects formed from primitive int values between values -128 to 127


Before diving much deep into this topic i’ll recommend you to have knowledge of Autoboxing and unboxing in java - How it works internally in detail with 10 examples

Java caching >
Java caches all the Integer objects formed from primitive int values between values -128 to 127 (including -128 and 127). This caching is done in Integer.valueOf method. Hence, Integer.valueOf method returns cached object(if any) for primitive int values between values -128 to 127.  
Note : 127 is just the maximum default value, it’s not the fixed value it can be changed (scroll down post).



Why java do such caching?
  • Java caches the Integer object of most frequently used int values.
  • For optimizing performance.
  • For reducing number of objects formed in heap.

Many java experts have criticized this caching process because it enforces kind of different mechanism for different int values (i.e. for int values not in range of -128 to 127). Example -
Integer i1 = 150; //no caching, hence 1st Integer object is formed
Integer i2 = 150; //no caching was done, hence 2nd Integer object is formed
When any primitive int value like 150 is used no caching is performed and two Integer objects are formed.

But, when any primitive int value like 3 is used caching will be performed and two Integer objects will be formed in above program.


Compare Integer with Integer (where both Integers are formed without using new operator) using == operator for comparison >
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Autoboxing{
   public static void main(String[] args) {
          Integer i1 = 3; //caches Integer object
          Integer i2 = 3; //returns cached Integer object
          System.out.println(i1 == i2); //true
   }
}
/*output
true
*/


Now, in the above program,
Integer i1 = 3;
3 is autoboxed to Integer (new Integer object is formed) in Integer.valueOf(3) method, new Integer object is cached (as 3 is between -128 to 127) and is referred by Integer i1.

Integer i2 = 3;
Integer.valueOf(3) method returns cached object and now same Integer object is being referred by Integer i1 and i2.
Hence, only one Integer object will be formed in above program.

Java compiler will convert above code into -
public class Autoboxing{
   public static void main(String[] args) {
          Integer i1 = Integer.valueOf(3); //caches Integer object
          Integer i2 = Integer.valueOf(3); //returns cached Integer object
          System.out.println(i1 == i2); //true
   }
}

Example where no caching done >

Let’s discuss Example mentioned above -
Integer i1 = 150; //no caching, hence 1st Integer object is formed
Integer i2 = 150; //no caching was done, hence 2nd Integer object is formed
When any primitive int value like 150 is used no caching is performed and two Integer objects are formed.
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class Autoboxing{
   public static void main(String[] args) {
          Integer i1 = 150; //no caching, hence 1st Integer object is formed
          Integer i2 = 150; //no caching was done, hence 2nd Integer object is formed
          System.out.println(i1 == i2); //false
   }
}
/*output
false
*/




How java maintains caching for Integer class >

As I mentioned 127 is just the default value, it’s not the fixed value it can be changed (scroll down post).

First let’s take a look at valueOf method in java.lang.Integer class >

java.lang.Integer.IntegerCache is the class which provides cache support for Integer Objects.
Value of IntegerCache.low is -127 and
Value of IntegerCache.high is configurable.
During VM initialization IntegerCache.high reads value from sun.misc.VM class (By default this value is 128).



how can we change maximum default caching value >
By setting VM param  -XX:AutoBoxCacheMax = option we can change maximum caching value.


RELATED LINKS>

Autoboxing and unboxing in java - How it works internally in detail with 10 examples- Widening, AutoBoxing and Var-args

10 points on why we are still using primitive data types in java ? When to use primitive type and wrapper classes?

Method overloading with Widening, Autoboxing and Var-args - Who beats whom in java?

Change boxing or Unboxing Setting in eclipse in java - Advantages


No comments:

Post a Comment