< 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 wraps a value of primitive type {@code
 43  * short} in an object.  An object of type {@code Short} contains a
 44  * single field whose type is {@code short}.
 45  *
 46  * <p>In addition, this class provides several methods for converting
 47  * a {@code short} to a {@code String} and a {@code String} to a
 48  * {@code short}, as well as other constants and methods useful when
 49  * dealing with a {@code short}.
 50  *
 51  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 52  * class; programmers should treat instances that are
 53  * {@linkplain #equals(Object) equal} as interchangeable and should not
 54  * use instances for synchronization, or unpredictable behavior may
 55  * occur. For example, in a future release, synchronization may fail.
 56  *
 57  * @author  Nakul Saraiya
 58  * @author  Joseph D. Darcy
 59  * @see     java.lang.Number
 60  * @since   1.1
 61  */

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

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

276     public static Short valueOf(short s) {
277         final int offset = 128;
278         int sAsInt = s;
279         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
280             return ShortCache.cache[sAsInt + offset];
281         }
282         return new Short(s);
283     }
284 
285     /**
286      * Decodes a {@code String} into a {@code Short}.
287      * Accepts decimal, hexadecimal, and octal numbers given by
288      * the following grammar:
289      *
290      * <blockquote>
291      * <dl>
292      * <dt><i>DecodableString:</i>
293      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
294      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
295      * <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 wraps a value of primitive type {@code
 43  * short} in an object.  An object of type {@code Short} contains a
 44  * single field whose type is {@code short}.
 45  *
 46  * <p>In addition, this class provides several methods for converting
 47  * a {@code short} to a {@code String} and a {@code String} to a
 48  * {@code short}, as well as other constants and methods useful when
 49  * dealing with a {@code short}.
 50  *
 51  * <p>This is a <a href="{@docRoot}/java.base/java/lang/doc-files/ValueBased.html">value-based</a>
 52  * class; programmers should treat instances that are
 53  * {@linkplain #equals(Object) equal} as interchangeable and should not
 54  * use instances for synchronization, or unpredictable behavior may
 55  * occur. For example, in a future release, synchronization may fail.
 56  *
 57  * @author  Nakul Saraiya
 58  * @author  Joseph D. Darcy
 59  * @see     java.lang.Number
 60  * @since   1.1
 61  */
 62 @jdk.internal.MigratedValueClass
 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     @DeserializeConstructor
278     public static Short valueOf(short s) {
279         final int offset = 128;
280         int sAsInt = s;
281         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
282             return ShortCache.cache[sAsInt + offset];
283         }
284         return new Short(s);
285     }
286 
287     /**
288      * Decodes a {@code String} into a {@code Short}.
289      * Accepts decimal, hexadecimal, and octal numbers given by
290      * the following grammar:
291      *
292      * <blockquote>
293      * <dl>
294      * <dt><i>DecodableString:</i>
295      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
296      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
297      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
< prev index next >