First look at a piece of code (using JDK 5), as follows:
[HTML]View PlainCopy
- public class Hello
- {
- public static void Main (string[] args)
- {
- int A = + , B = 1000;
- System.out.println (a = = b);
- Integer C = N, d = 1000;
- System.out.println (c = = d);
- Integer e = + , f = 100;
- System.out.println (E = = f);
- }
- }
Output Result:
[HTML]View PlainCopy
- True
- False
- True
the Java Language specification, 3rd Edition wrote:
[HTML]View PlainCopy
- To save memory, for two instances of the following wrapper objects, when their base values are the same, they are always = =:
- Boolean
- Byte
- Character, \u0000-\u007f (7f is the decimal 127)
- Integer,-128-127
View the JDK source code as follows:
[Java]View PlainCopy
- /**
- * Cache to support the object identity semantics of autoboxing for values between
- * -128 and 127 (inclusive) as required by JLS.
- *
- * The cache is initialized on first usage. During VM Initialization the
- * Getandremovecacheproperties method May is used to get and remove any system
- * Properites that configure the cache size. At this time, the size of the
- * Cache May is controlled by the VM Option-xx:autoboxcachemax=<size>.
- */
- //value of Java.lang.Integer.IntegerCache.high property (obtained during VM init)
- private static String Integercachehighpropvalue;
- static void Getandremovecacheproperties () {
- if (!sun.misc.vm.isbooted ()) {
- Properties props = System.getproperties ();
- Integercachehighpropvalue =
- (String) Props.remove ("Java.lang.Integer.IntegerCache.high");
- if (integercachehighpropvalue! = null)
- System.setproperties (props); //Remove from System props
- }
- }
- private static class Integercache {
- static final int high;
- static final Integer cache[];
- Static {
- final int low =-128;
- //High value is configured by property
- int h = 127;
- if (integercachehighpropvalue! = null) {
- //Use Long.decode This avoid invoking methods that
- //Require Integer ' s autoboxing cache to be initialized
- int i = Long.decode (integercachehighpropvalue). Intvalue ();
- i = Math.max (i, 127);
- //Maximum array size is Integer.max_value
- h = math.min (i, Integer.max_value--low);
- }
- High = h;
- cache = New integer[(high-low) + 1];
- int j = Low;
- For (int k = 0; k < cache.length; k++) //Buffer room data
- CACHE[K] = new Integer (j + +);
- }
- Private Integercache () {}
- }
- /**
- * Returns A <tt>Integer</tt> instance representing the specified
- * <tt>int</tt> value.
- * If A new <tt>Integer</tt> instance isn't required, this method
- * Should generally be used on preference to the constructor
- * {@link #Integer (int)}, as this method was likely to yield
- * Significantly better space and time performance by caching
- * Frequently requested values.
- *
- * @param i an <code>int</code> value.
- * @return A <tt>Integer</tt> instance representing <tt>i</tt>.
- * @since 1.5
- */
- public static Integer valueOf (int i) {
- if (i >=-&& i <= integercache.high)
- return integercache.cache[i + 128];
- Else
- return new Integer (i);
- }
The integercache here has a static integer array that, when the class loads, creates a 128 to 127 integer object and saves it in the cache array once the program calls the ValueOf method, if the value of I is 128 to 127 The integer object is taken directly between the cache buffer array.
And look at the other wrappers:
- Boolean: (full cache)
- Byte: (Cache All)
- Character (<= 127 cache)
- Short (-128-127 cache)
- Long (-128-127 cache)
- Float (no cache)
- Doulbe (no cache)
Also for the garbage collector:
[Java]View PlainCopy
- Integer i = 100;
- i = null; Would not do any of the object available for GC at all.
The code here does not have objects that conform to the garbage collector's condition, where I is given null, but it points to an integer object in the cache, and the cache is not assigned null, so an integer (100) object is still present.
If I is greater than 127 or less than-128, the object it points to will be eligible for garbage collection:
[Java]View PlainCopy
- Integer i = 10000;
- i = null; Would make the newly created an Integer object available for GC.
Cache for Java integer classes (RPM)