< 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     public static final Class<Short> TYPE = Class.getPrimitiveClass("short");

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 {@linkplain #equals(Object) equal}
 54  * as interchangeable and should not use instances for synchronization, mutexes, or
 55  * with {@linkplain java.lang.ref.Reference object references}.
 56  *
 57  * <div class="preview-block">
 58  *      <div class="preview-comment">
 59  *          When preview features are enabled, {@code Short} is a {@linkplain Class#isValue value class}.
 60  *          Use of value class instances for synchronization, mutexes, or with
 61  *          {@linkplain java.lang.ref.Reference object references} result in
 62  *          {@link IdentityException}.
 63  *      </div>
 64  * </div>
 65  *
 66  * @author  Nakul Saraiya
 67  * @author  Joseph D. Darcy
 68  * @see     java.lang.Number
 69  * @since   1.1
 70  */
 71 @jdk.internal.MigratedValueClass
 72 @jdk.internal.ValueBased
 73 public final class Short extends Number implements Comparable<Short>, Constable {
 74 
 75     /**
 76      * A constant holding the minimum value a {@code short} can
 77      * have, -2<sup>15</sup>.
 78      */
 79     public static final short   MIN_VALUE = -32768;
 80 
 81     /**
 82      * A constant holding the maximum value a {@code short} can
 83      * have, 2<sup>15</sup>-1.
 84      */
 85     public static final short   MAX_VALUE = 32767;
 86 
 87     /**
 88      * The {@code Class} instance representing the primitive type
 89      * {@code short}.
 90      */
 91     public static final Class<Short> TYPE = Class.getPrimitiveClass("short");

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