< 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


  57      * have, -2<sup>31</sup>.
  58      */
  59     @Native public static final int   MIN_VALUE = 0x80000000;
  60 
  61     /**
  62      * A constant holding the maximum value an {@code int} can
  63      * have, 2<sup>31</sup>-1.
  64      */
  65     @Native public static final int   MAX_VALUE = 0x7fffffff;
  66 
  67     /**
  68      * The {@code Class} instance representing the primitive type
  69      * {@code int}.
  70      *
  71      * @since   1.1
  72      */
  73     @SuppressWarnings("unchecked")
  74     public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
  75 
  76     /**





  77      * All possible chars for representing a number as a String
  78      */
  79     static final char[] digits = {
  80         '0' , '1' , '2' , '3' , '4' , '5' ,
  81         '6' , '7' , '8' , '9' , 'a' , 'b' ,
  82         'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
  83         'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  84         'o' , 'p' , 'q' , 'r' , 's' , 't' ,
  85         'u' , 'v' , 'w' , 'x' , 'y' , 'z'
  86     };
  87 
  88     /**
  89      * Returns a string representation of the first argument in the
  90      * radix specified by the second argument.
  91      *
  92      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
  93      * or larger than {@code Character.MAX_RADIX}, then the radix
  94      * {@code 10} is used instead.
  95      *
  96      * <p>If the first argument is negative, the first element of the


 932 
 933         static {
 934             // high value may be configured by property
 935             int h = 127;
 936             String integerCacheHighPropValue =
 937                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
 938             if (integerCacheHighPropValue != null) {
 939                 try {
 940                     int i = parseInt(integerCacheHighPropValue);
 941                     i = Math.max(i, 127);
 942                     // Maximum array size is Integer.MAX_VALUE
 943                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
 944                 } catch( NumberFormatException nfe) {
 945                     // If the property cannot be parsed into an int, ignore it.
 946                 }
 947             }
 948             high = h;
 949 
 950             cache = new Integer[(high - low) + 1];
 951             int j = low;
 952             for(int k = 0; k < cache.length; k++)
 953                 cache[k] = new Integer(j++);


 954 
 955             // range [-128, 127] must be interned (JLS7 5.1.7)
 956             assert IntegerCache.high >= 127;
 957         }
 958 
 959         private IntegerCache() {}
 960     }
 961 
 962     /**
 963      * Returns an {@code Integer} instance representing the specified
 964      * {@code int} value.  If a new {@code Integer} instance is not
 965      * required, this method should generally be used in preference to
 966      * the constructor {@link #Integer(int)}, as this method is likely
 967      * to yield significantly better space and time performance by
 968      * caching frequently requested values.
 969      *
 970      * This method will always cache values in the range -128 to 127,
 971      * inclusive, and may cache other values outside of this range.
 972      *
 973      * @param  i an {@code int} value.




  57      * have, -2<sup>31</sup>.
  58      */
  59     @Native public static final int   MIN_VALUE = 0x80000000;
  60 
  61     /**
  62      * A constant holding the maximum value an {@code int} can
  63      * have, 2<sup>31</sup>-1.
  64      */
  65     @Native public static final int   MAX_VALUE = 0x7fffffff;
  66 
  67     /**
  68      * The {@code Class} instance representing the primitive type
  69      * {@code int}.
  70      *
  71      * @since   1.1
  72      */
  73     @SuppressWarnings("unchecked")
  74     public static final Class<Integer>  TYPE = (Class<Integer>) Class.getPrimitiveClass("int");
  75 
  76     /**
  77      * Zero {@code Integer} constant.
  78      */
  79     private static final Integer ZERO = new Integer(0);
  80 
  81     /**
  82      * All possible chars for representing a number as a String
  83      */
  84     static final char[] digits = {
  85         '0' , '1' , '2' , '3' , '4' , '5' ,
  86         '6' , '7' , '8' , '9' , 'a' , 'b' ,
  87         'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
  88         'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
  89         'o' , 'p' , 'q' , 'r' , 's' , 't' ,
  90         'u' , 'v' , 'w' , 'x' , 'y' , 'z'
  91     };
  92 
  93     /**
  94      * Returns a string representation of the first argument in the
  95      * radix specified by the second argument.
  96      *
  97      * <p>If the radix is smaller than {@code Character.MIN_RADIX}
  98      * or larger than {@code Character.MAX_RADIX}, then the radix
  99      * {@code 10} is used instead.
 100      *
 101      * <p>If the first argument is negative, the first element of the


 937 
 938         static {
 939             // high value may be configured by property
 940             int h = 127;
 941             String integerCacheHighPropValue =
 942                 sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");
 943             if (integerCacheHighPropValue != null) {
 944                 try {
 945                     int i = parseInt(integerCacheHighPropValue);
 946                     i = Math.max(i, 127);
 947                     // Maximum array size is Integer.MAX_VALUE
 948                     h = Math.min(i, Integer.MAX_VALUE - (-low) -1);
 949                 } catch( NumberFormatException nfe) {
 950                     // If the property cannot be parsed into an int, ignore it.
 951                 }
 952             }
 953             high = h;
 954 
 955             cache = new Integer[(high - low) + 1];
 956             int j = low;
 957             for(int k = 0; k < cache.length; k++) {
 958                 int val = j++;
 959                 cache[k] = (val == 0) ? ZERO : new Integer(val);
 960             }
 961 
 962             // range [-128, 127] must be interned (JLS7 5.1.7)
 963             assert IntegerCache.high >= 127;
 964         }
 965 
 966         private IntegerCache() {}
 967     }
 968 
 969     /**
 970      * Returns an {@code Integer} instance representing the specified
 971      * {@code int} value.  If a new {@code Integer} instance is not
 972      * required, this method should generally be used in preference to
 973      * the constructor {@link #Integer(int)}, as this method is likely
 974      * to yield significantly better space and time performance by
 975      * caching frequently requested values.
 976      *
 977      * This method will always cache values in the range -128 to 127,
 978      * inclusive, and may cache other values outside of this range.
 979      *
 980      * @param  i an {@code int} value.


< prev index next >