src/java.base/share/classes/java/lang/Short.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File 8076112 Sdiff src/java.base/share/classes/java/lang

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

Print this page




   8  * particular file as subject to the "Classpath" exception as provided
   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 /**
  29  * The {@code Short} class wraps a value of primitive type {@code
  30  * short} in an object.  An object of type {@code Short} contains a
  31  * single field whose type is {@code short}.
  32  *
  33  * <p>In addition, this class provides several methods for converting
  34  * a {@code short} to a {@code String} and a {@code String} to a
  35  * {@code short}, as well as other constants and methods useful when
  36  * dealing with a {@code short}.
  37  *
  38  * @author  Nakul Saraiya
  39  * @author  Joseph D. Darcy
  40  * @see     java.lang.Number
  41  * @since   1.1
  42  */
  43 public final class Short extends Number implements Comparable<Short> {
  44 
  45     /**
  46      * A constant holding the minimum value a {@code short} can
  47      * have, -2<sup>15</sup>.


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

 230     public static Short valueOf(short s) {
 231         final int offset = 128;
 232         int sAsInt = s;
 233         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
 234             return ShortCache.cache[sAsInt + offset];
 235         }
 236         return new Short(s);
 237     }
 238 
 239     /**
 240      * Decodes a {@code String} into a {@code Short}.
 241      * Accepts decimal, hexadecimal, and octal numbers given by
 242      * the following grammar:
 243      *
 244      * <blockquote>
 245      * <dl>
 246      * <dt><i>DecodableString:</i>
 247      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
 248      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
 249      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>


 317      *          does not contain a parsable {@code short}.
 318      * @see     java.lang.Short#parseShort(java.lang.String, int)
 319      */
 320     public Short(String s) throws NumberFormatException {
 321         this.value = parseShort(s, 10);
 322     }
 323 
 324     /**
 325      * Returns the value of this {@code Short} as a {@code byte} after
 326      * a narrowing primitive conversion.
 327      * @jls 5.1.3 Narrowing Primitive Conversions
 328      */
 329     public byte byteValue() {
 330         return (byte)value;
 331     }
 332 
 333     /**
 334      * Returns the value of this {@code Short} as a
 335      * {@code short}.
 336      */

 337     public short shortValue() {
 338         return value;
 339     }
 340 
 341     /**
 342      * Returns the value of this {@code Short} as an {@code int} after
 343      * a widening primitive conversion.
 344      * @jls 5.1.2 Widening Primitive Conversions
 345      */
 346     public int intValue() {
 347         return (int)value;
 348     }
 349 
 350     /**
 351      * Returns the value of this {@code Short} as a {@code long} after
 352      * a widening primitive conversion.
 353      * @jls 5.1.2 Widening Primitive Conversions
 354      */
 355     public long longValue() {
 356         return (long)value;


 470      */
 471     public static final int SIZE = 16;
 472 
 473     /**
 474      * The number of bytes used to represent a {@code short} value in two's
 475      * complement binary form.
 476      *
 477      * @since 1.8
 478      */
 479     public static final int BYTES = SIZE / Byte.SIZE;
 480 
 481     /**
 482      * Returns the value obtained by reversing the order of the bytes in the
 483      * two's complement representation of the specified {@code short} value.
 484      *
 485      * @param i the value whose bytes are to be reversed
 486      * @return the value obtained by reversing (or, equivalently, swapping)
 487      *     the bytes in the specified {@code short} value.
 488      * @since 1.5
 489      */

 490     public static short reverseBytes(short i) {
 491         return (short) (((i & 0xFF00) >> 8) | (i << 8));
 492     }
 493 
 494 
 495     /**
 496      * Converts the argument to an {@code int} by an unsigned
 497      * conversion.  In an unsigned conversion to an {@code int}, the
 498      * high-order 16 bits of the {@code int} are zero and the
 499      * low-order 16 bits are equal to the bits of the {@code short} argument.
 500      *
 501      * Consequently, zero and positive {@code short} values are mapped
 502      * to a numerically equal {@code int} value and negative {@code
 503      * short} values are mapped to an {@code int} value equal to the
 504      * input plus 2<sup>16</sup>.
 505      *
 506      * @param  x the value to convert to an unsigned {@code int}
 507      * @return the argument converted to {@code int} by an unsigned
 508      *         conversion
 509      * @since 1.8




   8  * particular file as subject to the "Classpath" exception as provided
   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.HotSpotIntrinsicCandidate;
  29 
  30 /**
  31  * The {@code Short} class wraps a value of primitive type {@code
  32  * short} in an object.  An object of type {@code Short} contains a
  33  * single field whose type is {@code short}.
  34  *
  35  * <p>In addition, this class provides several methods for converting
  36  * a {@code short} to a {@code String} and a {@code String} to a
  37  * {@code short}, as well as other constants and methods useful when
  38  * dealing with a {@code short}.
  39  *
  40  * @author  Nakul Saraiya
  41  * @author  Joseph D. Darcy
  42  * @see     java.lang.Number
  43  * @since   1.1
  44  */
  45 public final class Short extends Number implements Comparable<Short> {
  46 
  47     /**
  48      * A constant holding the minimum value a {@code short} can
  49      * have, -2<sup>15</sup>.


 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
 233     public static Short valueOf(short s) {
 234         final int offset = 128;
 235         int sAsInt = s;
 236         if (sAsInt >= -128 && sAsInt <= 127) { // must cache
 237             return ShortCache.cache[sAsInt + offset];
 238         }
 239         return new Short(s);
 240     }
 241 
 242     /**
 243      * Decodes a {@code String} into a {@code Short}.
 244      * Accepts decimal, hexadecimal, and octal numbers given by
 245      * the following grammar:
 246      *
 247      * <blockquote>
 248      * <dl>
 249      * <dt><i>DecodableString:</i>
 250      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
 251      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
 252      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>


 320      *          does not contain a parsable {@code short}.
 321      * @see     java.lang.Short#parseShort(java.lang.String, int)
 322      */
 323     public Short(String s) throws NumberFormatException {
 324         this.value = parseShort(s, 10);
 325     }
 326 
 327     /**
 328      * Returns the value of this {@code Short} as a {@code byte} after
 329      * a narrowing primitive conversion.
 330      * @jls 5.1.3 Narrowing Primitive Conversions
 331      */
 332     public byte byteValue() {
 333         return (byte)value;
 334     }
 335 
 336     /**
 337      * Returns the value of this {@code Short} as a
 338      * {@code short}.
 339      */
 340     @HotSpotIntrinsicCandidate
 341     public short shortValue() {
 342         return value;
 343     }
 344 
 345     /**
 346      * Returns the value of this {@code Short} as an {@code int} after
 347      * a widening primitive conversion.
 348      * @jls 5.1.2 Widening Primitive Conversions
 349      */
 350     public int intValue() {
 351         return (int)value;
 352     }
 353 
 354     /**
 355      * Returns the value of this {@code Short} as a {@code long} after
 356      * a widening primitive conversion.
 357      * @jls 5.1.2 Widening Primitive Conversions
 358      */
 359     public long longValue() {
 360         return (long)value;


 474      */
 475     public static final int SIZE = 16;
 476 
 477     /**
 478      * The number of bytes used to represent a {@code short} value in two's
 479      * complement binary form.
 480      *
 481      * @since 1.8
 482      */
 483     public static final int BYTES = SIZE / Byte.SIZE;
 484 
 485     /**
 486      * Returns the value obtained by reversing the order of the bytes in the
 487      * two's complement representation of the specified {@code short} value.
 488      *
 489      * @param i the value whose bytes are to be reversed
 490      * @return the value obtained by reversing (or, equivalently, swapping)
 491      *     the bytes in the specified {@code short} value.
 492      * @since 1.5
 493      */
 494     @HotSpotIntrinsicCandidate
 495     public static short reverseBytes(short i) {
 496         return (short) (((i & 0xFF00) >> 8) | (i << 8));
 497     }
 498 
 499 
 500     /**
 501      * Converts the argument to an {@code int} by an unsigned
 502      * conversion.  In an unsigned conversion to an {@code int}, the
 503      * high-order 16 bits of the {@code int} are zero and the
 504      * low-order 16 bits are equal to the bits of the {@code short} argument.
 505      *
 506      * Consequently, zero and positive {@code short} values are mapped
 507      * to a numerically equal {@code int} value and negative {@code
 508      * short} values are mapped to an {@code int} value equal to the
 509      * input plus 2<sup>16</sup>.
 510      *
 511      * @param  x the value to convert to an unsigned {@code int}
 512      * @return the argument converted to {@code int} by an unsigned
 513      *         conversion
 514      * @since 1.8


src/java.base/share/classes/java/lang/Short.java
Index Unified diffs Context diffs Sdiffs Patch New Old Previous File Next File