码迷,mamicode.com
首页 > 编程语言 > 详细

java.lang.Byte 类源码浅析

时间:2018-04-07 01:14:52      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:tin   OWIN   ceo   war   opera   any   lse   result   equal   

  • Byte 类字节,属于Number。
      1 public final class Byte extends Number implements Comparable<Byte> {
      2 
      3     /**
      4      * A constant holding the minimum value a {@code byte} can
      5      * have, -2<sup>7</sup>.
      6      */
      7     public static final byte   MIN_VALUE = -128;
      8 
      9     /**
     10      * A constant holding the maximum value a {@code byte} can
     11      * have, 2<sup>7</sup>-1.
     12      */
     13     public static final byte   MAX_VALUE = 127;
     14 
     15     /**
     16      * The {@code Class} instance representing the primitive type
     17      * {@code byte}.
     18      */
     19     @SuppressWarnings("unchecked")
     20     public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
     21 
     22     /**
     23      * Returns a new {@code String} object representing the
     24      * specified {@code byte}. The radix is assumed to be 10.
     25      *
     26      * @param b the {@code byte} to be converted
     27      * @return the string representation of the specified {@code byte}
     28      * @see java.lang.Integer#toString(int)
     29      */
     30     public static String toString(byte b) {
     31         return Integer.toString((int)b, 10);
     32     }
     33 
     34     private static class ByteCache {
     35         private ByteCache(){}
     36 
     37         static final Byte cache[] = new Byte[-(-128) + 127 + 1];
     38 
     39         static {
     40             for(int i = 0; i < cache.length; i++)
     41                 cache[i] = new Byte((byte)(i - 128));
     42         }
     43     }
     44 
     45     /**
     46      * Returns a {@code Byte} instance representing the specified
     47      * {@code byte} value.
     48      * If a new {@code Byte} instance is not required, this method
     49      * should generally be used in preference to the constructor
     50      * {@link #Byte(byte)}, as this method is likely to yield
     51      * significantly better space and time performance since
     52      * all byte values are cached.
     53      *
     54      * @param  b a byte value.
     55      * @return a {@code Byte} instance representing {@code b}.
     56      * @since  1.5
     57      */
     58     public static Byte valueOf(byte b) {
     59         final int offset = 128;
     60         return ByteCache.cache[(int)b + offset];
     61     }
     62 
     63     /**
     64      * Parses the string argument as a signed {@code byte} in the
     65      * radix specified by the second argument. The characters in the
     66      * string must all be digits, of the specified radix (as
     67      * determined by whether {@link java.lang.Character#digit(char,
     68      * int)} returns a nonnegative value) except that the first
     69      * character may be an ASCII minus sign {@code ‘-‘}
     70      * ({@code ‘\u005Cu002D‘}) to indicate a negative value or an
     71      * ASCII plus sign {@code ‘+‘} ({@code ‘\u005Cu002B‘}) to
     72      * indicate a positive value.  The resulting {@code byte} value is
     73      * returned.
     74      *
     75      * <p>An exception of type {@code NumberFormatException} is
     76      * thrown if any of the following situations occurs:
     77      * <ul>
     78      * <li> The first argument is {@code null} or is a string of
     79      * length zero.
     80      *
     81      * <li> The radix is either smaller than {@link
     82      * java.lang.Character#MIN_RADIX} or larger than {@link
     83      * java.lang.Character#MAX_RADIX}.
     84      *
     85      * <li> Any character of the string is not a digit of the
     86      * specified radix, except that the first character may be a minus
     87      * sign {@code ‘-‘} ({@code ‘\u005Cu002D‘}) or plus sign
     88      * {@code ‘+‘} ({@code ‘\u005Cu002B‘}) provided that the
     89      * string is longer than length 1.
     90      *
     91      * <li> The value represented by the string is not a value of type
     92      * {@code byte}.
     93      * </ul>
     94      *
     95      * @param s         the {@code String} containing the
     96      *                  {@code byte}
     97      *                  representation to be parsed
     98      * @param radix     the radix to be used while parsing {@code s}
     99      * @return          the {@code byte} value represented by the string
    100      *                   argument in the specified radix
    101      * @throws          NumberFormatException If the string does
    102      *                  not contain a parsable {@code byte}.
    103      */
    104     public static byte parseByte(String s, int radix)
    105         throws NumberFormatException {
    106         int i = Integer.parseInt(s, radix);
    107         if (i < MIN_VALUE || i > MAX_VALUE)
    108             throw new NumberFormatException(
    109                 "Value out of range. Value:\"" + s + "\" Radix:" + radix);
    110         return (byte)i;
    111     }
    112 
    113     /**
    114      * Parses the string argument as a signed decimal {@code
    115      * byte}. The characters in the string must all be decimal digits,
    116      * except that the first character may be an ASCII minus sign
    117      * {@code ‘-‘} ({@code ‘\u005Cu002D‘}) to indicate a negative
    118      * value or an ASCII plus sign {@code ‘+‘}
    119      * ({@code ‘\u005Cu002B‘}) to indicate a positive value. The
    120      * resulting {@code byte} value is returned, exactly as if the
    121      * argument and the radix 10 were given as arguments to the {@link
    122      * #parseByte(java.lang.String, int)} method.
    123      *
    124      * @param s         a {@code String} containing the
    125      *                  {@code byte} representation to be parsed
    126      * @return          the {@code byte} value represented by the
    127      *                  argument in decimal
    128      * @throws          NumberFormatException if the string does not
    129      *                  contain a parsable {@code byte}.
    130      */
    131     public static byte parseByte(String s) throws NumberFormatException {
    132         return parseByte(s, 10);
    133     }
    134 
    135     /**
    136      * Returns a {@code Byte} object holding the value
    137      * extracted from the specified {@code String} when parsed
    138      * with the radix given by the second argument. The first argument
    139      * is interpreted as representing a signed {@code byte} in
    140      * the radix specified by the second argument, exactly as if the
    141      * argument were given to the {@link #parseByte(java.lang.String,
    142      * int)} method. The result is a {@code Byte} object that
    143      * represents the {@code byte} value specified by the string.
    144      *
    145      * <p> In other words, this method returns a {@code Byte} object
    146      * equal to the value of:
    147      *
    148      * <blockquote>
    149      * {@code new Byte(Byte.parseByte(s, radix))}
    150      * </blockquote>
    151      *
    152      * @param s         the string to be parsed
    153      * @param radix     the radix to be used in interpreting {@code s}
    154      * @return          a {@code Byte} object holding the value
    155      *                  represented by the string argument in the
    156      *                  specified radix.
    157      * @throws          NumberFormatException If the {@code String} does
    158      *                  not contain a parsable {@code byte}.
    159      */
    160     public static Byte valueOf(String s, int radix)
    161         throws NumberFormatException {
    162         return valueOf(parseByte(s, radix));
    163     }
    164 
    165     /**
    166      * Returns a {@code Byte} object holding the value
    167      * given by the specified {@code String}. The argument is
    168      * interpreted as representing a signed decimal {@code byte},
    169      * exactly as if the argument were given to the {@link
    170      * #parseByte(java.lang.String)} method. The result is a
    171      * {@code Byte} object that represents the {@code byte}
    172      * value specified by the string.
    173      *
    174      * <p> In other words, this method returns a {@code Byte} object
    175      * equal to the value of:
    176      *
    177      * <blockquote>
    178      * {@code new Byte(Byte.parseByte(s))}
    179      * </blockquote>
    180      *
    181      * @param s         the string to be parsed
    182      * @return          a {@code Byte} object holding the value
    183      *                  represented by the string argument
    184      * @throws          NumberFormatException If the {@code String} does
    185      *                  not contain a parsable {@code byte}.
    186      */
    187     public static Byte valueOf(String s) throws NumberFormatException {
    188         return valueOf(s, 10);
    189     }
    190 
    191     /**
    192      * Decodes a {@code String} into a {@code Byte}.
    193      * Accepts decimal, hexadecimal, and octal numbers given by
    194      * the following grammar:
    195      *
    196      * <blockquote>
    197      * <dl>
    198      * <dt><i>DecodableString:</i>
    199      * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
    200      * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
    201      * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
    202      * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
    203      * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
    204      *
    205      * <dt><i>Sign:</i>
    206      * <dd>{@code -}
    207      * <dd>{@code +}
    208      * </dl>
    209      * </blockquote>
    210      *
    211      * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
    212      * are as defined in section 3.10.1 of
    213      * <cite>The Java&trade; Language Specification</cite>,
    214      * except that underscores are not accepted between digits.
    215      *
    216      * <p>The sequence of characters following an optional
    217      * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
    218      * "{@code #}", or leading zero) is parsed as by the {@code
    219      * Byte.parseByte} method with the indicated radix (10, 16, or 8).
    220      * This sequence of characters must represent a positive value or
    221      * a {@link NumberFormatException} will be thrown.  The result is
    222      * negated if first character of the specified {@code String} is
    223      * the minus sign.  No whitespace characters are permitted in the
    224      * {@code String}.
    225      *
    226      * @param     nm the {@code String} to decode.
    227      * @return   a {@code Byte} object holding the {@code byte}
    228      *          value represented by {@code nm}
    229      * @throws  NumberFormatException  if the {@code String} does not
    230      *            contain a parsable {@code byte}.
    231      * @see java.lang.Byte#parseByte(java.lang.String, int)
    232      */
    233     public static Byte decode(String nm) throws NumberFormatException {
    234         int i = Integer.decode(nm);
    235         if (i < MIN_VALUE || i > MAX_VALUE)
    236             throw new NumberFormatException(
    237                     "Value " + i + " out of range from input " + nm);
    238         return valueOf((byte)i);
    239     }
    240 
    241     /**
    242      * The value of the {@code Byte}.
    243      *
    244      * @serial
    245      */
    246     private final byte value;
    247 
    248     /**
    249      * Constructs a newly allocated {@code Byte} object that
    250      * represents the specified {@code byte} value.
    251      *
    252      * @param value     the value to be represented by the
    253      *                  {@code Byte}.
    254      */
    255     public Byte(byte value) {
    256         this.value = value;
    257     }
    258 
    259     /**
    260      * Constructs a newly allocated {@code Byte} object that
    261      * represents the {@code byte} value indicated by the
    262      * {@code String} parameter. The string is converted to a
    263      * {@code byte} value in exactly the manner used by the
    264      * {@code parseByte} method for radix 10.
    265      *
    266      * @param s         the {@code String} to be converted to a
    267      *                  {@code Byte}
    268      * @throws           NumberFormatException If the {@code String}
    269      *                  does not contain a parsable {@code byte}.
    270      * @see        java.lang.Byte#parseByte(java.lang.String, int)
    271      */
    272     public Byte(String s) throws NumberFormatException {
    273         this.value = parseByte(s, 10);
    274     }
    275 
    276     /**
    277      * Returns the value of this {@code Byte} as a
    278      * {@code byte}.
    279      */
    280     public byte byteValue() {
    281         return value;
    282     }
    283 
    284     /**
    285      * Returns the value of this {@code Byte} as a {@code short} after
    286      * a widening primitive conversion.
    287      * @jls 5.1.2 Widening Primitive Conversions
    288      */
    289     public short shortValue() {
    290         return (short)value;
    291     }
    292 
    293     /**
    294      * Returns the value of this {@code Byte} as an {@code int} after
    295      * a widening primitive conversion.
    296      * @jls 5.1.2 Widening Primitive Conversions
    297      */
    298     public int intValue() {
    299         return (int)value;
    300     }
    301 
    302     /**
    303      * Returns the value of this {@code Byte} as a {@code long} after
    304      * a widening primitive conversion.
    305      * @jls 5.1.2 Widening Primitive Conversions
    306      */
    307     public long longValue() {
    308         return (long)value;
    309     }
    310 
    311     /**
    312      * Returns the value of this {@code Byte} as a {@code float} after
    313      * a widening primitive conversion.
    314      * @jls 5.1.2 Widening Primitive Conversions
    315      */
    316     public float floatValue() {
    317         return (float)value;
    318     }
    319 
    320     /**
    321      * Returns the value of this {@code Byte} as a {@code double}
    322      * after a widening primitive conversion.
    323      * @jls 5.1.2 Widening Primitive Conversions
    324      */
    325     public double doubleValue() {
    326         return (double)value;
    327     }
    328 
    329     /**
    330      * Returns a {@code String} object representing this
    331      * {@code Byte}‘s value.  The value is converted to signed
    332      * decimal representation and returned as a string, exactly as if
    333      * the {@code byte} value were given as an argument to the
    334      * {@link java.lang.Byte#toString(byte)} method.
    335      *
    336      * @return  a string representation of the value of this object in
    337      *          base&nbsp;10.
    338      */
    339     public String toString() {
    340         return Integer.toString((int)value);
    341     }
    342 
    343     /**
    344      * Returns a hash code for this {@code Byte}; equal to the result
    345      * of invoking {@code intValue()}.
    346      *
    347      * @return a hash code value for this {@code Byte}
    348      */
    349     @Override
    350     public int hashCode() {
    351         return Byte.hashCode(value);
    352     }
    353 
    354     /**
    355      * Returns a hash code for a {@code byte} value; compatible with
    356      * {@code Byte.hashCode()}.
    357      *
    358      * @param value the value to hash
    359      * @return a hash code value for a {@code byte} value.
    360      * @since 1.8
    361      */
    362     public static int hashCode(byte value) {
    363         return (int)value;
    364     }
    365 
    366     /**
    367      * Compares this object to the specified object.  The result is
    368      * {@code true} if and only if the argument is not
    369      * {@code null} and is a {@code Byte} object that
    370      * contains the same {@code byte} value as this object.
    371      *
    372      * @param obj       the object to compare with
    373      * @return          {@code true} if the objects are the same;
    374      *                  {@code false} otherwise.
    375      */
    376     public boolean equals(Object obj) {
    377         if (obj instanceof Byte) {
    378             return value == ((Byte)obj).byteValue();
    379         }
    380         return false;
    381     }
    382 
    383     /**
    384      * Compares two {@code Byte} objects numerically.
    385      *
    386      * @param   anotherByte   the {@code Byte} to be compared.
    387      * @return  the value {@code 0} if this {@code Byte} is
    388      *          equal to the argument {@code Byte}; a value less than
    389      *          {@code 0} if this {@code Byte} is numerically less
    390      *          than the argument {@code Byte}; and a value greater than
    391      *           {@code 0} if this {@code Byte} is numerically
    392      *           greater than the argument {@code Byte} (signed
    393      *           comparison).
    394      * @since   1.2
    395      */
    396     public int compareTo(Byte anotherByte) {
    397         return compare(this.value, anotherByte.value);
    398     }
    399 
    400     /**
    401      * Compares two {@code byte} values numerically.
    402      * The value returned is identical to what would be returned by:
    403      * <pre>
    404      *    Byte.valueOf(x).compareTo(Byte.valueOf(y))
    405      * </pre>
    406      *
    407      * @param  x the first {@code byte} to compare
    408      * @param  y the second {@code byte} to compare
    409      * @return the value {@code 0} if {@code x == y};
    410      *         a value less than {@code 0} if {@code x < y}; and
    411      *         a value greater than {@code 0} if {@code x > y}
    412      * @since 1.7
    413      */
    414     public static int compare(byte x, byte y) {
    415         return x - y;
    416     }
    417 
    418     /**
    419      * Converts the argument to an {@code int} by an unsigned
    420      * conversion.  In an unsigned conversion to an {@code int}, the
    421      * high-order 24 bits of the {@code int} are zero and the
    422      * low-order 8 bits are equal to the bits of the {@code byte} argument.
    423      *
    424      * Consequently, zero and positive {@code byte} values are mapped
    425      * to a numerically equal {@code int} value and negative {@code
    426      * byte} values are mapped to an {@code int} value equal to the
    427      * input plus 2<sup>8</sup>.
    428      *
    429      * @param  x the value to convert to an unsigned {@code int}
    430      * @return the argument converted to {@code int} by an unsigned
    431      *         conversion
    432      * @since 1.8
    433      */
    434     public static int toUnsignedInt(byte x) {
    435         return ((int) x) & 0xff;
    436     }
    437 
    438     /**
    439      * Converts the argument to a {@code long} by an unsigned
    440      * conversion.  In an unsigned conversion to a {@code long}, the
    441      * high-order 56 bits of the {@code long} are zero and the
    442      * low-order 8 bits are equal to the bits of the {@code byte} argument.
    443      *
    444      * Consequently, zero and positive {@code byte} values are mapped
    445      * to a numerically equal {@code long} value and negative {@code
    446      * byte} values are mapped to a {@code long} value equal to the
    447      * input plus 2<sup>8</sup>.
    448      *
    449      * @param  x the value to convert to an unsigned {@code long}
    450      * @return the argument converted to {@code long} by an unsigned
    451      *         conversion
    452      * @since 1.8
    453      */
    454     public static long toUnsignedLong(byte x) {
    455         return ((long) x) & 0xffL;
    456     }
    457 
    458 
    459     /**
    460      * The number of bits used to represent a {@code byte} value in two‘s
    461      * complement binary form.
    462      *
    463      * @since 1.5
    464      */
    465     public static final int SIZE = 8;
    466 
    467     /**
    468      * The number of bytes used to represent a {@code byte} value in two‘s
    469      * complement binary form.
    470      *
    471      * @since 1.8
    472      */
    473     public static final int BYTES = SIZE / Byte.SIZE;
    474 
    475     /** use serialVersionUID from JDK 1.1. for interoperability */
    476     private static final long serialVersionUID = -7183698231559129828L;
    477 }

     

java.lang.Byte 类源码浅析

标签:tin   OWIN   ceo   war   opera   any   lse   result   equal   

原文地址:https://www.cnblogs.com/aben-blog/p/8729468.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!