< prev index next >

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

Print this page
rev 51374 : 8209120: Archive the Integer.IntegerCache
Reviewed-by: jiangli, alanb, plevart


 980      *             as an integer.
 981      */
 982     public static Integer valueOf(String s) throws NumberFormatException {
 983         return Integer.valueOf(parseInt(s, 10));
 984     }
 985 
 986     /**
 987      * Cache to support the object identity semantics of autoboxing for values between
 988      * -128 and 127 (inclusive) as required by JLS.
 989      *
 990      * The cache is initialized on first usage.  The size of the cache
 991      * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
 992      * During VM initialization, java.lang.Integer.IntegerCache.high property
 993      * may be set and saved in the private system properties in the
 994      * jdk.internal.misc.VM class.
 995      */
 996 
 997     private static class IntegerCache {
 998         static final int low = -128;
 999         static final int high;
1000         static final Integer cache[];

1001 
1002         static {
1003             // high value may be configured by property
1004             int h = 127;
1005             String integerCacheHighPropValue =
1006                 VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
1007             if (integerCacheHighPropValue != null) {
1008                 try {
1009                     int i = parseInt(integerCacheHighPropValue);
1010                     i = Math.max(i, 127);
1011                     // Maximum array size is Integer.MAX_VALUE
1012                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
1013                 } catch( NumberFormatException nfe) {
1014                     // If the property cannot be parsed into an int, ignore it.
1015                 }
1016             }
1017             high = h;
1018 
1019             cache = new Integer[(high - low) + 1];
1020             int j = low;
1021             for(int k = 0; k < cache.length; k++)
1022                 cache[k] = new Integer(j++);
1023 









1024             // range [-128, 127] must be interned (JLS7 5.1.7)
1025             assert IntegerCache.high >= 127;
1026         }
1027 
1028         private IntegerCache() {}
1029     }
1030 
1031     /**
1032      * Returns an {@code Integer} instance representing the specified
1033      * {@code int} value.  If a new {@code Integer} instance is not
1034      * required, this method should generally be used in preference to
1035      * the constructor {@link #Integer(int)}, as this method is likely
1036      * to yield significantly better space and time performance by
1037      * caching frequently requested values.
1038      *
1039      * This method will always cache values in the range -128 to 127,
1040      * inclusive, and may cache other values outside of this range.
1041      *
1042      * @param  i an {@code int} value.
1043      * @return an {@code Integer} instance representing {@code i}.




 980      *             as an integer.
 981      */
 982     public static Integer valueOf(String s) throws NumberFormatException {
 983         return Integer.valueOf(parseInt(s, 10));
 984     }
 985 
 986     /**
 987      * Cache to support the object identity semantics of autoboxing for values between
 988      * -128 and 127 (inclusive) as required by JLS.
 989      *
 990      * The cache is initialized on first usage.  The size of the cache
 991      * may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.
 992      * During VM initialization, java.lang.Integer.IntegerCache.high property
 993      * may be set and saved in the private system properties in the
 994      * jdk.internal.misc.VM class.
 995      */
 996 
 997     private static class IntegerCache {
 998         static final int low = -128;
 999         static final int high;
1000         static final Integer[] cache;
1001         static Integer[] archivedCache;
1002 
1003         static {
1004             // high value may be configured by property
1005             int h = 127;
1006             String integerCacheHighPropValue =
1007                 VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
1008             if (integerCacheHighPropValue != null) {
1009                 try {
1010                     int i = parseInt(integerCacheHighPropValue);
1011                     i = Math.max(i, 127);
1012                     // Maximum array size is Integer.MAX_VALUE
1013                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
1014                 } catch( NumberFormatException nfe) {
1015                     // If the property cannot be parsed into an int, ignore it.
1016                 }
1017             }
1018             high = h;
1019 
1020             // Load IntegerCache.archivedCache from archive, if possible
1021             VM.initializeFromArchive(IntegerCache.class);
1022             int size = (high - low) + 1;

1023 
1024             // Use the archived cache if it exists and is large enough
1025             if (archivedCache == null || size > archivedCache.length) {
1026                 Integer[] c = new Integer[size];
1027                 int j = low;
1028                 for(int k = 0; k < c.length; k++)
1029                     c[k] = new Integer(j++);
1030                 archivedCache = c;
1031             }
1032             cache = archivedCache;
1033             // range [-128, 127] must be interned (JLS7 5.1.7)
1034             assert IntegerCache.high >= 127;
1035         }
1036 
1037         private IntegerCache() {}
1038     }
1039 
1040     /**
1041      * Returns an {@code Integer} instance representing the specified
1042      * {@code int} value.  If a new {@code Integer} instance is not
1043      * required, this method should generally be used in preference to
1044      * the constructor {@link #Integer(int)}, as this method is likely
1045      * to yield significantly better space and time performance by
1046      * caching frequently requested values.
1047      *
1048      * This method will always cache values in the range -128 to 127,
1049      * inclusive, and may cache other values outside of this range.
1050      *
1051      * @param  i an {@code int} value.
1052      * @return an {@code Integer} instance representing {@code i}.


< prev index next >