< prev index next >

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

Print this page

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang;
 27 
 28 import jdk.internal.misc.CDS;

 29 import jdk.internal.vm.annotation.IntrinsicCandidate;
 30 import jdk.internal.vm.annotation.Stable;
 31 
 32 import java.lang.constant.Constable;
 33 import java.lang.constant.DynamicConstantDesc;
 34 import java.util.Optional;
 35 
 36 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;
 37 import static java.lang.constant.ConstantDescs.CD_int;
 38 import static java.lang.constant.ConstantDescs.CD_short;
 39 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
 40 
 41 /**
 42  * The {@code Short} class is the {@linkplain
 43  * java.lang##wrapperClass wrapper class} for values of the primitive
 44  * type {@code short}. An object of type {@code Short} contains a
 45  * single field whose type is {@code short}.
 46  *
 47  * <p>In addition, this class provides several methods for converting
 48  * a {@code short} to a {@code String} and a {@code String} to a
 49  * {@code short}, as well as other constants and methods useful when
 50  * dealing with a {@code short}.
 51  *
 52  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 53  * class; programmers should treat instances that are
 54  * {@linkplain #equals(Object) equal} as interchangeable and should not
 55  * use instances for synchronization, or unpredictable behavior may
 56  * occur. For example, in a future release, synchronization may fail.
 57  *
 58  * @author  Nakul Saraiya
 59  * @author  Joseph D. Darcy
 60  * @see     java.lang.Number
 61  * @since   1.1
 62  */

 63 @jdk.internal.ValueBased
 64 public final class Short extends Number implements Comparable<Short>, Constable {
 65 
 66     /**
 67      * A constant holding the minimum value a {@code short} can
 68      * have, -2<sup>15</sup>.
 69      */
 70     public static final short   MIN_VALUE = -32768;
 71 
 72     /**
 73      * A constant holding the maximum value a {@code short} can
 74      * have, 2<sup>15</sup>-1.
 75      */
 76     public static final short   MAX_VALUE = 32767;
 77 
 78     /**
 79      * The {@code Class} instance representing the primitive type
 80      * {@code short}.
 81      */
 82     @SuppressWarnings("unchecked")

257         }
258     }
259 
260     /**
261      * Returns a {@code Short} instance representing the specified
262      * {@code short} value.
263      * If a new {@code Short} instance is not required, this method
264      * should generally be used in preference to the constructor
265      * {@link #Short(short)}, as this method is likely to yield
266      * significantly better space and time performance by caching
267      * frequently requested values.
268      *
269      * This method will always cache values in the range -128 to 127,
270      * inclusive, and may cache other values outside of this range.
271      *
272      * @param  s a short value.
273      * @return a {@code Short} instance representing {@code s}.
274      * @since  1.5
275      */
276     @IntrinsicCandidate

277     public static Short valueOf(short s) {
278         final int offset = 128;
279         int sAsInt = s;
280         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
281             return ShortCache.cache[sAsInt + offset];
282         }
283         return new Short(s);
284     }
285 
286     /**
287      * Decodes a {@code String} into a {@code Short}.
288      * Accepts decimal, hexadecimal, and octal numbers given by
289      * the following grammar:
290      *
291      * <blockquote>
292      * <dl>
293      * <dt><i>DecodableString:</i>
294      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
295      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
296      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>

  9  * by Oracle in the LICENSE file that accompanied this code.
 10  *
 11  * This code is distributed in the hope that it will be useful, but WITHOUT
 12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 14  * version 2 for more details (a copy is included in the LICENSE file that
 15  * accompanied this code).
 16  *
 17  * You should have received a copy of the GNU General Public License version
 18  * 2 along with this work; if not, write to the Free Software Foundation,
 19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 20  *
 21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 22  * or visit www.oracle.com if you need additional information or have any
 23  * questions.
 24  */
 25 
 26 package java.lang;
 27 
 28 import jdk.internal.misc.CDS;
 29 import jdk.internal.value.DeserializeConstructor;
 30 import jdk.internal.vm.annotation.IntrinsicCandidate;
 31 import jdk.internal.vm.annotation.Stable;
 32 
 33 import java.lang.constant.Constable;
 34 import java.lang.constant.DynamicConstantDesc;
 35 import java.util.Optional;
 36 
 37 import static java.lang.constant.ConstantDescs.BSM_EXPLICIT_CAST;

 38 import static java.lang.constant.ConstantDescs.CD_short;
 39 import static java.lang.constant.ConstantDescs.DEFAULT_NAME;
 40 
 41 /**
 42  * The {@code Short} class is the {@linkplain
 43  * java.lang##wrapperClass wrapper class} for values of the primitive
 44  * type {@code short}. An object of type {@code Short} contains a
 45  * single field whose type is {@code short}.
 46  *
 47  * <p>In addition, this class provides several methods for converting
 48  * a {@code short} to a {@code String} and a {@code String} to a
 49  * {@code short}, as well as other constants and methods useful when
 50  * dealing with a {@code short}.
 51  *
 52  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 53  * class; programmers should treat instances that are
 54  * {@linkplain #equals(Object) equal} as interchangeable and should not
 55  * use instances for synchronization, or unpredictable behavior may
 56  * occur. For example, in a future release, synchronization may fail.
 57  *
 58  * @author  Nakul Saraiya
 59  * @author  Joseph D. Darcy
 60  * @see     java.lang.Number
 61  * @since   1.1
 62  */
 63 @jdk.internal.MigratedValueClass
 64 @jdk.internal.ValueBased
 65 public final class Short extends Number implements Comparable<Short>, Constable {
 66 
 67     /**
 68      * A constant holding the minimum value a {@code short} can
 69      * have, -2<sup>15</sup>.
 70      */
 71     public static final short   MIN_VALUE = -32768;
 72 
 73     /**
 74      * A constant holding the maximum value a {@code short} can
 75      * have, 2<sup>15</sup>-1.
 76      */
 77     public static final short   MAX_VALUE = 32767;
 78 
 79     /**
 80      * The {@code Class} instance representing the primitive type
 81      * {@code short}.
 82      */
 83     @SuppressWarnings("unchecked")

258         }
259     }
260 
261     /**
262      * Returns a {@code Short} instance representing the specified
263      * {@code short} value.
264      * If a new {@code Short} instance is not required, this method
265      * should generally be used in preference to the constructor
266      * {@link #Short(short)}, as this method is likely to yield
267      * significantly better space and time performance by caching
268      * frequently requested values.
269      *
270      * This method will always cache values in the range -128 to 127,
271      * inclusive, and may cache other values outside of this range.
272      *
273      * @param  s a short value.
274      * @return a {@code Short} instance representing {@code s}.
275      * @since  1.5
276      */
277     @IntrinsicCandidate
278     @DeserializeConstructor
279     public static Short valueOf(short s) {
280         final int offset = 128;
281         int sAsInt = s;
282         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
283             return ShortCache.cache[sAsInt + offset];
284         }
285         return new Short(s);
286     }
287 
288     /**
289      * Decodes a {@code String} into a {@code Short}.
290      * Accepts decimal, hexadecimal, and octal numbers given by
291      * the following grammar:
292      *
293      * <blockquote>
294      * <dl>
295      * <dt><i>DecodableString:</i>
296      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
297      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
298      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
< prev index next >