< prev index next >

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

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


  46 
  47     /**
  48      * A constant holding the minimum value a {@code short} can
  49      * have, -2<sup>15</sup>.
  50      */
  51     public static final short   MIN_VALUE = -32768;
  52 
  53     /**
  54      * A constant holding the maximum value a {@code short} can
  55      * have, 2<sup>15</sup>-1.
  56      */
  57     public static final short   MAX_VALUE = 32767;
  58 
  59     /**
  60      * The {@code Class} instance representing the primitive type
  61      * {@code short}.
  62      */
  63     @SuppressWarnings("unchecked")
  64     public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");
  65 


  66     /**
  67      * Returns a new {@code String} object representing the
  68      * specified {@code short}. The radix is assumed to be 10.
  69      *
  70      * @param s the {@code short} to be converted
  71      * @return the string representation of the specified {@code short}
  72      * @see java.lang.Integer#toString(int)
  73      */
  74     public static String toString(short s) {
  75         return Integer.toString((int)s, 10);
  76     }
  77 
  78     /**
  79      * Parses the string argument as a signed {@code short} in the
  80      * radix specified by the second argument. The characters in the
  81      * string must all be digits, of the specified radix (as
  82      * determined by whether {@link java.lang.Character#digit(char,
  83      * int)} returns a nonnegative value) except that the first
  84      * character may be an ASCII minus sign {@code '-'}
  85      * ({@code '\u005Cu002D'}) to indicate a negative value or an


 191      * <blockquote>
 192      *  {@code new Short(Short.parseShort(s))}
 193      * </blockquote>
 194      *
 195      * @param s the string to be parsed
 196      * @return  a {@code Short} object holding the value
 197      *          represented by the string argument
 198      * @throws  NumberFormatException If the {@code String} does
 199      *          not contain a parsable {@code short}.
 200      */
 201     public static Short valueOf(String s) throws NumberFormatException {
 202         return valueOf(s, 10);
 203     }
 204 
 205     private static class ShortCache {
 206         private ShortCache(){}
 207 
 208         static final Short cache[] = new Short[-(-128) + 127 + 1];
 209 
 210         static {
 211             for(int i = 0; i < cache.length; i++)
 212                 cache[i] = new Short((short)(i - 128));


 213         }
 214     }
 215 
 216     /**
 217      * Returns a {@code Short} instance representing the specified
 218      * {@code short} value.
 219      * If a new {@code Short} instance is not required, this method
 220      * should generally be used in preference to the constructor
 221      * {@link #Short(short)}, as this method is likely to yield
 222      * significantly better space and time performance by caching
 223      * frequently requested values.
 224      *
 225      * This method will always cache values in the range -128 to 127,
 226      * inclusive, and may cache other values outside of this range.
 227      *
 228      * @param  s a short value.
 229      * @return a {@code Short} instance representing {@code s}.
 230      * @since  1.5
 231      */
 232     @HotSpotIntrinsicCandidate




  46 
  47     /**
  48      * A constant holding the minimum value a {@code short} can
  49      * have, -2<sup>15</sup>.
  50      */
  51     public static final short   MIN_VALUE = -32768;
  52 
  53     /**
  54      * A constant holding the maximum value a {@code short} can
  55      * have, 2<sup>15</sup>-1.
  56      */
  57     public static final short   MAX_VALUE = 32767;
  58 
  59     /**
  60      * The {@code Class} instance representing the primitive type
  61      * {@code short}.
  62      */
  63     @SuppressWarnings("unchecked")
  64     public static final Class<Short>    TYPE = (Class<Short>) Class.getPrimitiveClass("short");
  65 
  66     private static final Short ZERO = new Short((short)0);
  67 
  68     /**
  69      * Returns a new {@code String} object representing the
  70      * specified {@code short}. The radix is assumed to be 10.
  71      *
  72      * @param s the {@code short} to be converted
  73      * @return the string representation of the specified {@code short}
  74      * @see java.lang.Integer#toString(int)
  75      */
  76     public static String toString(short s) {
  77         return Integer.toString((int)s, 10);
  78     }
  79 
  80     /**
  81      * Parses the string argument as a signed {@code short} in the
  82      * radix specified by the second argument. The characters in the
  83      * string must all be digits, of the specified radix (as
  84      * determined by whether {@link java.lang.Character#digit(char,
  85      * int)} returns a nonnegative value) except that the first
  86      * character may be an ASCII minus sign {@code '-'}
  87      * ({@code '\u005Cu002D'}) to indicate a negative value or an


 193      * <blockquote>
 194      *  {@code new Short(Short.parseShort(s))}
 195      * </blockquote>
 196      *
 197      * @param s the string to be parsed
 198      * @return  a {@code Short} object holding the value
 199      *          represented by the string argument
 200      * @throws  NumberFormatException If the {@code String} does
 201      *          not contain a parsable {@code short}.
 202      */
 203     public static Short valueOf(String s) throws NumberFormatException {
 204         return valueOf(s, 10);
 205     }
 206 
 207     private static class ShortCache {
 208         private ShortCache(){}
 209 
 210         static final Short cache[] = new Short[-(-128) + 127 + 1];
 211 
 212         static {
 213             for(int i = 0; i < cache.length; i++) {
 214                 int val = i - 128;
 215                 cache[i] = (val == 0) ? ZERO : new Short((short)val);
 216             }
 217         }
 218     }
 219 
 220     /**
 221      * Returns a {@code Short} instance representing the specified
 222      * {@code short} value.
 223      * If a new {@code Short} instance is not required, this method
 224      * should generally be used in preference to the constructor
 225      * {@link #Short(short)}, as this method is likely to yield
 226      * significantly better space and time performance by caching
 227      * frequently requested values.
 228      *
 229      * This method will always cache values in the range -128 to 127,
 230      * inclusive, and may cache other values outside of this range.
 231      *
 232      * @param  s a short value.
 233      * @return a {@code Short} instance representing {@code s}.
 234      * @since  1.5
 235      */
 236     @HotSpotIntrinsicCandidate


< prev index next >