< prev index next >

src/java.base/share/classes/java/lang/Integer.java

Print this page
rev 13019 : 8141678: sun.invoke.util.Wrapper eagerly initializes all integral type caches
Reviewed-by: TBD

@@ -72,10 +72,15 @@
      */
     @SuppressWarnings("unchecked")
     public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
 
     /**
+     * Zero {@code Integer} constant.
+     */
+    private static final Integer ZERO = new Integer(0);
+
+    /**
      * All possible chars for representing a number as a String
      */
     static final char[] digits = {
         '0' , '1' , '2' , '3' , '4' , '5' ,
         '6' , '7' , '8' , '9' , 'a' , 'b' ,

@@ -947,12 +952,14 @@
             }
             high = h;
 
             cache = new Integer[(high - low) + 1];
             int j = low;
-            for(int k = 0; k < cache.length; k++)
-                cache[k] = new Integer(j++);
+            for(int k = 0; k < cache.length; k++) {
+                int val = j++;
+                cache[k] = (val == 0) ? ZERO : new Integer(val);
+            }
 
             // range [-128, 127] must be interned (JLS7 5.1.7)
             assert IntegerCache.high >= 127;
         }
 
< prev index next >