jdk/src/share/classes/java/lang/Byte.java
author martin
Mon, 10 Mar 2008 14:32:51 -0700
changeset 48 dc5744ca15ea
parent 2 90ce3da70b43
child 3224 3aa65803ae07
permissions -rw-r--r--
4960438: (process) Need IO redirection API for subprocesses Reviewed-by: alanb, iris
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2006 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.lang;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * The {@code Byte} class wraps a value of primitive type {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * in an object.  An object of type {@code Byte} contains a single
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * field whose type is {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>In addition, this class provides several methods for converting
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * a {@code byte} to a {@code String} and a {@code String} to a {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * byte}, as well as other constants and methods useful when dealing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * with a {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author  Nakul Saraiya
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Joseph D. Darcy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see     java.lang.Number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @since   JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
public final class Byte extends Number implements Comparable<Byte> {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     * A constant holding the minimum value a {@code byte} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * have, -2<sup>7</sup>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static final byte   MIN_VALUE = -128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * A constant holding the maximum value a {@code byte} can
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * have, 2<sup>7</sup>-1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static final byte   MAX_VALUE = 127;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * The {@code Class} instance representing the primitive type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static final Class<Byte>     TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Returns a new {@code String} object representing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * specified {@code byte}. The radix is assumed to be 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param b the {@code byte} to be converted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @return the string representation of the specified {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @see java.lang.Integer#toString(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    public static String toString(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return Integer.toString((int)b, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static class ByteCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        private ByteCache(){}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        static final Byte cache[] = new Byte[-(-128) + 127 + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            for(int i = 0; i < cache.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                cache[i] = new Byte((byte)(i - 128));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Returns a {@code Byte} instance representing the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * {@code byte} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * If a new {@code Byte} instance is not required, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * should generally be used in preference to the constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * {@link #Byte(byte)}, as this method is likely to yield
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * significantly better space and time performance by caching
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * frequently requested values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param  b a byte value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @return a {@code Byte} instance representing {@code b}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @since  1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public static Byte valueOf(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        final int offset = 128;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return ByteCache.cache[(int)b + offset];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * Parses the string argument as a signed {@code byte} in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * radix specified by the second argument. The characters in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * string must all be digits, of the specified radix (as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * determined by whether {@link java.lang.Character#digit(char,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * int)} returns a nonnegative value) except that the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * character may be an ASCII minus sign {@code '-'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * (<code>'&#92;u002D'</code>) to indicate a negative value or an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * indicate a positive value.  The resulting {@code byte} value is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <p>An exception of type {@code NumberFormatException} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * thrown if any of the following situations occurs:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <li> The first argument is {@code null} or is a string of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * length zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <li> The radix is either smaller than {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * java.lang.Character#MIN_RADIX} or larger than {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * java.lang.Character#MAX_RADIX}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <li> Any character of the string is not a digit of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * specified radix, except that the first character may be a minus
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * sign {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * string is longer than length 1.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <li> The value represented by the string is not a value of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param s         the {@code String} containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *                  {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *                  representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @param radix     the radix to be used while parsing {@code s}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @return          the {@code byte} value represented by the string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *                   argument in the specified radix
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @throws          NumberFormatException If the string does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *                  not contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    public static byte parseByte(String s, int radix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        int i = Integer.parseInt(s, radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (i < MIN_VALUE || i > MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw new NumberFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                "Value out of range. Value:\"" + s + "\" Radix:" + radix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        return (byte)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Parses the string argument as a signed decimal {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * byte}. The characters in the string must all be decimal digits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * except that the first character may be an ASCII minus sign
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * {@code '-'} (<code>'&#92;u002D'</code>) to indicate a negative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * value or an ASCII plus sign {@code '+'}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * (<code>'&#92;u002B'</code>) to indicate a positive value. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * resulting {@code byte} value is returned, exactly as if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * argument and the radix 10 were given as arguments to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * #parseByte(java.lang.String, int)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * @param s         a {@code String} containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *                  {@code byte} representation to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @return          the {@code byte} value represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *                  argument in decimal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @throws          NumberFormatException if the string does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *                  contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public static byte parseByte(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return parseByte(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * Returns a {@code Byte} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * extracted from the specified {@code String} when parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * with the radix given by the second argument. The first argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * is interpreted as representing a signed {@code byte} in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * the radix specified by the second argument, exactly as if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * argument were given to the {@link #parseByte(java.lang.String,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * int)} method. The result is a {@code Byte} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * represents the {@code byte} value specified by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <p> In other words, this method returns a {@code Byte} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * {@code new Byte(Byte.parseByte(s, radix))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param s         the string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @param radix     the radix to be used in interpreting {@code s}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * @return          a {@code Byte} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *                  represented by the string argument in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *                  specified radix.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * @throws          NumberFormatException If the {@code String} does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *                  not contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    public static Byte valueOf(String s, int radix)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        return new Byte(parseByte(s, radix));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Returns a {@code Byte} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * given by the specified {@code String}. The argument is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * interpreted as representing a signed decimal {@code byte},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * exactly as if the argument were given to the {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * #parseByte(java.lang.String)} method. The result is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * {@code Byte} object that represents the {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * value specified by the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <p> In other words, this method returns a {@code Byte} object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * equal to the value of:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * {@code new Byte(Byte.parseByte(s))}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param s         the string to be parsed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return          a {@code Byte} object holding the value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *                  represented by the string argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * @throws          NumberFormatException If the {@code String} does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *                  not contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    public static Byte valueOf(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        return valueOf(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * Decodes a {@code String} into a {@code Byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Accepts decimal, hexadecimal, and octal numbers given by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * the following grammar:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <dt><i>DecodableString:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <dt><i>Sign:</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <dd>{@code -}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <dd>{@code +}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * </blockquote>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">&sect;3.10.1</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * of the <a href="http://java.sun.com/docs/books/jls/html/">Java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * Language Specification</a>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * <p>The sequence of characters following an optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * "{@code #}", or leading zero) is parsed as by the {@code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * Byte.parseByte} method with the indicated radix (10, 16, or 8).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * This sequence of characters must represent a positive value or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * a {@link NumberFormatException} will be thrown.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * negated if first character of the specified {@code String} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * the minus sign.  No whitespace characters are permitted in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * {@code String}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param     nm the {@code String} to decode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @return   a {@code Byte} object holding the {@code byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *          value represented by {@code nm}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @throws  NumberFormatException  if the {@code String} does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *            contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @see java.lang.Byte#parseByte(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    public static Byte decode(String nm) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        int i = Integer.decode(nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        if (i < MIN_VALUE || i > MAX_VALUE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            throw new NumberFormatException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    "Value " + i + " out of range from input " + nm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return (byte)i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * The value of the {@code Byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    private final byte value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Constructs a newly allocated {@code Byte} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * represents the specified {@code byte} value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param value     the value to be represented by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     *                  {@code Byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    public Byte(byte value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * Constructs a newly allocated {@code Byte} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * represents the {@code byte} value indicated by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * {@code String} parameter. The string is converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * {@code byte} value in exactly the manner used by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * {@code parseByte} method for radix 10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @param s         the {@code String} to be converted to a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *                  {@code Byte}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @throws           NumberFormatException If the {@code String}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *                  does not contain a parsable {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @see        java.lang.Byte#parseByte(java.lang.String, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    public Byte(String s) throws NumberFormatException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        this.value = parseByte(s, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * Returns the value of this {@code Byte} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * {@code byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public byte byteValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        return value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * Returns the value of this {@code Byte} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * {@code short}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    public short shortValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return (short)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * Returns the value of this {@code Byte} as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * {@code int}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    public int intValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * Returns the value of this {@code Byte} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * {@code long}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    public long longValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        return (long)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Returns the value of this {@code Byte} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * {@code float}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public float floatValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return (float)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Returns the value of this {@code Byte} as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * {@code double}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public double doubleValue() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return (double)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Returns a {@code String} object representing this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * {@code Byte}'s value.  The value is converted to signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * decimal representation and returned as a string, exactly as if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * the {@code byte} value were given as an argument to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * {@link java.lang.Byte#toString(byte)} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return  a string representation of the value of this object in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *          base&nbsp;10.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        return String.valueOf((int)value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Returns a hash code for this {@code Byte}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    public int hashCode() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        return (int)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * Compares this object to the specified object.  The result is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * {@code true} if and only if the argument is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * {@code null} and is a {@code Byte} object that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * contains the same {@code byte} value as this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @param obj       the object to compare with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return          {@code true} if the objects are the same;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     *                  {@code false} otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public boolean equals(Object obj) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        if (obj instanceof Byte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            return value == ((Byte)obj).byteValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * Compares two {@code Byte} objects numerically.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * @param   anotherByte   the {@code Byte} to be compared.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * @return  the value {@code 0} if this {@code Byte} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     *          equal to the argument {@code Byte}; a value less than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *          {@code 0} if this {@code Byte} is numerically less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     *          than the argument {@code Byte}; and a value greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *           {@code 0} if this {@code Byte} is numerically
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *           greater than the argument {@code Byte} (signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *           comparison).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * @since   1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    public int compareTo(Byte anotherByte) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        return this.value - anotherByte.value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * The number of bits used to represent a {@code byte} value in two's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * complement binary form.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    public static final int SIZE = 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /** use serialVersionUID from JDK 1.1. for interoperability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    private static final long serialVersionUID = -7183698231559129828L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
}