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

Print this page




 202     private static class ShortCache {
 203         private ShortCache(){}
 204 
 205         static final Short cache[] = new Short[-(-128) + 127 + 1];
 206 
 207         static {
 208             for(int i = 0; i < cache.length; i++)
 209                 cache[i] = new Short((short)(i - 128));
 210         }
 211     }
 212 
 213     /**
 214      * Returns a {@code Short} instance representing the specified
 215      * {@code short} value.
 216      * If a new {@code Short} instance is not required, this method
 217      * should generally be used in preference to the constructor
 218      * {@link #Short(short)}, as this method is likely to yield
 219      * significantly better space and time performance by caching
 220      * frequently requested values.
 221      *



 222      * @param  s a short value.
 223      * @return a {@code Short} instance representing {@code s}.
 224      * @since  1.5
 225      */
 226     public static Short valueOf(short s) {
 227         final int offset = 128;
 228         int sAsInt = s;
 229         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
 230             return ShortCache.cache[sAsInt + offset];
 231         }
 232         return new Short(s);
 233     }
 234 
 235     /**
 236      * Decodes a {@code String} into a {@code Short}.
 237      * Accepts decimal, hexadecimal, and octal numbers given by
 238      * the following grammar:
 239      *
 240      * <blockquote>
 241      * <dl>




 202     private static class ShortCache {
 203         private ShortCache(){}
 204 
 205         static final Short cache[] = new Short[-(-128) + 127 + 1];
 206 
 207         static {
 208             for(int i = 0; i < cache.length; i++)
 209                 cache[i] = new Short((short)(i - 128));
 210         }
 211     }
 212 
 213     /**
 214      * Returns a {@code Short} instance representing the specified
 215      * {@code short} value.
 216      * If a new {@code Short} instance is not required, this method
 217      * should generally be used in preference to the constructor
 218      * {@link #Short(short)}, as this method is likely to yield
 219      * significantly better space and time performance by caching
 220      * frequently requested values.
 221      *
 222      * This method will always cache values in the range -128 to 127,
 223      * inclusive, and may cache other values outside of this range.
 224      *
 225      * @param  s a short value.
 226      * @return a {@code Short} instance representing {@code s}.
 227      * @since  1.5
 228      */
 229     public static Short valueOf(short s) {
 230         final int offset = 128;
 231         int sAsInt = s;
 232         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
 233             return ShortCache.cache[sAsInt + offset];
 234         }
 235         return new Short(s);
 236     }
 237 
 238     /**
 239      * Decodes a {@code String} into a {@code Short}.
 240      * Accepts decimal, hexadecimal, and octal numbers given by
 241      * the following grammar:
 242      *
 243      * <blockquote>
 244      * <dl>