0N/A/*
2362N/A * Copyright (c) 1996, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.lang;
0N/A
0N/A/**
0N/A *
0N/A * The {@code Byte} class wraps a value of primitive type {@code byte}
0N/A * in an object. An object of type {@code Byte} contains a single
0N/A * field whose type is {@code byte}.
0N/A *
0N/A * <p>In addition, this class provides several methods for converting
0N/A * a {@code byte} to a {@code String} and a {@code String} to a {@code
0N/A * byte}, as well as other constants and methods useful when dealing
0N/A * with a {@code byte}.
0N/A *
0N/A * @author Nakul Saraiya
0N/A * @author Joseph D. Darcy
0N/A * @see java.lang.Number
0N/A * @since JDK1.1
0N/A */
0N/Apublic final class Byte extends Number implements Comparable<Byte> {
0N/A
0N/A /**
0N/A * A constant holding the minimum value a {@code byte} can
0N/A * have, -2<sup>7</sup>.
0N/A */
0N/A public static final byte MIN_VALUE = -128;
0N/A
0N/A /**
0N/A * A constant holding the maximum value a {@code byte} can
0N/A * have, 2<sup>7</sup>-1.
0N/A */
0N/A public static final byte MAX_VALUE = 127;
0N/A
0N/A /**
0N/A * The {@code Class} instance representing the primitive type
0N/A * {@code byte}.
0N/A */
0N/A public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");
0N/A
0N/A /**
0N/A * Returns a new {@code String} object representing the
0N/A * specified {@code byte}. The radix is assumed to be 10.
0N/A *
0N/A * @param b the {@code byte} to be converted
0N/A * @return the string representation of the specified {@code byte}
0N/A * @see java.lang.Integer#toString(int)
0N/A */
0N/A public static String toString(byte b) {
0N/A return Integer.toString((int)b, 10);
0N/A }
0N/A
0N/A private static class ByteCache {
0N/A private ByteCache(){}
0N/A
0N/A static final Byte cache[] = new Byte[-(-128) + 127 + 1];
0N/A
0N/A static {
0N/A for(int i = 0; i < cache.length; i++)
0N/A cache[i] = new Byte((byte)(i - 128));
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Byte} instance representing the specified
0N/A * {@code byte} value.
0N/A * If a new {@code Byte} instance is not required, this method
0N/A * should generally be used in preference to the constructor
0N/A * {@link #Byte(byte)}, as this method is likely to yield
1390N/A * significantly better space and time performance since
1390N/A * all byte values are cached.
0N/A *
0N/A * @param b a byte value.
0N/A * @return a {@code Byte} instance representing {@code b}.
0N/A * @since 1.5
0N/A */
0N/A public static Byte valueOf(byte b) {
0N/A final int offset = 128;
0N/A return ByteCache.cache[(int)b + offset];
0N/A }
0N/A
0N/A /**
0N/A * Parses the string argument as a signed {@code byte} in the
0N/A * radix specified by the second argument. The characters in the
0N/A * string must all be digits, of the specified radix (as
0N/A * determined by whether {@link java.lang.Character#digit(char,
0N/A * int)} returns a nonnegative value) except that the first
0N/A * character may be an ASCII minus sign {@code '-'}
0N/A * (<code>'&#92;u002D'</code>) to indicate a negative value or an
0N/A * ASCII plus sign {@code '+'} (<code>'&#92;u002B'</code>) to
0N/A * indicate a positive value. The resulting {@code byte} value is
0N/A * returned.
0N/A *
0N/A * <p>An exception of type {@code NumberFormatException} is
0N/A * thrown if any of the following situations occurs:
0N/A * <ul>
0N/A * <li> The first argument is {@code null} or is a string of
0N/A * length zero.
0N/A *
0N/A * <li> The radix is either smaller than {@link
0N/A * java.lang.Character#MIN_RADIX} or larger than {@link
0N/A * java.lang.Character#MAX_RADIX}.
0N/A *
0N/A * <li> Any character of the string is not a digit of the
0N/A * specified radix, except that the first character may be a minus
0N/A * sign {@code '-'} (<code>'&#92;u002D'</code>) or plus sign
0N/A * {@code '+'} (<code>'&#92;u002B'</code>) provided that the
0N/A * string is longer than length 1.
0N/A *
0N/A * <li> The value represented by the string is not a value of type
0N/A * {@code byte}.
0N/A * </ul>
0N/A *
0N/A * @param s the {@code String} containing the
0N/A * {@code byte}
0N/A * representation to be parsed
0N/A * @param radix the radix to be used while parsing {@code s}
0N/A * @return the {@code byte} value represented by the string
0N/A * argument in the specified radix
0N/A * @throws NumberFormatException If the string does
0N/A * not contain a parsable {@code byte}.
0N/A */
0N/A public static byte parseByte(String s, int radix)
0N/A throws NumberFormatException {
0N/A int i = Integer.parseInt(s, radix);
0N/A if (i < MIN_VALUE || i > MAX_VALUE)
0N/A throw new NumberFormatException(
0N/A "Value out of range. Value:\"" + s + "\" Radix:" + radix);
0N/A return (byte)i;
0N/A }
0N/A
0N/A /**
0N/A * Parses the string argument as a signed decimal {@code
0N/A * byte}. The characters in the string must all be decimal digits,
0N/A * except that the first character may be an ASCII minus sign
0N/A * {@code '-'} (<code>'&#92;u002D'</code>) to indicate a negative
0N/A * value or an ASCII plus sign {@code '+'}
0N/A * (<code>'&#92;u002B'</code>) to indicate a positive value. The
0N/A * resulting {@code byte} value is returned, exactly as if the
0N/A * argument and the radix 10 were given as arguments to the {@link
0N/A * #parseByte(java.lang.String, int)} method.
0N/A *
0N/A * @param s a {@code String} containing the
0N/A * {@code byte} representation to be parsed
0N/A * @return the {@code byte} value represented by the
0N/A * argument in decimal
0N/A * @throws NumberFormatException if the string does not
0N/A * contain a parsable {@code byte}.
0N/A */
0N/A public static byte parseByte(String s) throws NumberFormatException {
0N/A return parseByte(s, 10);
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Byte} object holding the value
0N/A * extracted from the specified {@code String} when parsed
0N/A * with the radix given by the second argument. The first argument
0N/A * is interpreted as representing a signed {@code byte} in
0N/A * the radix specified by the second argument, exactly as if the
0N/A * argument were given to the {@link #parseByte(java.lang.String,
0N/A * int)} method. The result is a {@code Byte} object that
0N/A * represents the {@code byte} value specified by the string.
0N/A *
0N/A * <p> In other words, this method returns a {@code Byte} object
0N/A * equal to the value of:
0N/A *
0N/A * <blockquote>
0N/A * {@code new Byte(Byte.parseByte(s, radix))}
0N/A * </blockquote>
0N/A *
0N/A * @param s the string to be parsed
0N/A * @param radix the radix to be used in interpreting {@code s}
0N/A * @return a {@code Byte} object holding the value
0N/A * represented by the string argument in the
0N/A * specified radix.
0N/A * @throws NumberFormatException If the {@code String} does
0N/A * not contain a parsable {@code byte}.
0N/A */
0N/A public static Byte valueOf(String s, int radix)
0N/A throws NumberFormatException {
1722N/A return valueOf(parseByte(s, radix));
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code Byte} object holding the value
0N/A * given by the specified {@code String}. The argument is
0N/A * interpreted as representing a signed decimal {@code byte},
0N/A * exactly as if the argument were given to the {@link
0N/A * #parseByte(java.lang.String)} method. The result is a
0N/A * {@code Byte} object that represents the {@code byte}
0N/A * value specified by the string.
0N/A *
0N/A * <p> In other words, this method returns a {@code Byte} object
0N/A * equal to the value of:
0N/A *
0N/A * <blockquote>
0N/A * {@code new Byte(Byte.parseByte(s))}
0N/A * </blockquote>
0N/A *
0N/A * @param s the string to be parsed
0N/A * @return a {@code Byte} object holding the value
0N/A * represented by the string argument
0N/A * @throws NumberFormatException If the {@code String} does
0N/A * not contain a parsable {@code byte}.
0N/A */
0N/A public static Byte valueOf(String s) throws NumberFormatException {
0N/A return valueOf(s, 10);
0N/A }
0N/A
0N/A /**
0N/A * Decodes a {@code String} into a {@code Byte}.
0N/A * Accepts decimal, hexadecimal, and octal numbers given by
0N/A * the following grammar:
0N/A *
0N/A * <blockquote>
0N/A * <dl>
0N/A * <dt><i>DecodableString:</i>
0N/A * <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>
0N/A * <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>
0N/A * <p>
0N/A * <dt><i>Sign:</i>
0N/A * <dd>{@code -}
0N/A * <dd>{@code +}
0N/A * </dl>
0N/A * </blockquote>
0N/A *
0N/A * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>
4008N/A * are as defined in section 3.10.1 of
4008N/A * <cite>The Java&trade; Language Specification</cite>,
4008N/A * except that underscores are not accepted between digits.
0N/A *
0N/A * <p>The sequence of characters following an optional
0N/A * sign and/or radix specifier ("{@code 0x}", "{@code 0X}",
0N/A * "{@code #}", or leading zero) is parsed as by the {@code
0N/A * Byte.parseByte} method with the indicated radix (10, 16, or 8).
0N/A * This sequence of characters must represent a positive value or
0N/A * a {@link NumberFormatException} will be thrown. The result is
0N/A * negated if first character of the specified {@code String} is
0N/A * the minus sign. No whitespace characters are permitted in the
0N/A * {@code String}.
0N/A *
0N/A * @param nm the {@code String} to decode.
0N/A * @return a {@code Byte} object holding the {@code byte}
0N/A * value represented by {@code nm}
0N/A * @throws NumberFormatException if the {@code String} does not
0N/A * contain a parsable {@code byte}.
0N/A * @see java.lang.Byte#parseByte(java.lang.String, int)
0N/A */
0N/A public static Byte decode(String nm) throws NumberFormatException {
0N/A int i = Integer.decode(nm);
0N/A if (i < MIN_VALUE || i > MAX_VALUE)
0N/A throw new NumberFormatException(
0N/A "Value " + i + " out of range from input " + nm);
1722N/A return valueOf((byte)i);
0N/A }
0N/A
0N/A /**
0N/A * The value of the {@code Byte}.
0N/A *
0N/A * @serial
0N/A */
0N/A private final byte value;
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Byte} object that
0N/A * represents the specified {@code byte} value.
0N/A *
0N/A * @param value the value to be represented by the
0N/A * {@code Byte}.
0N/A */
0N/A public Byte(byte value) {
0N/A this.value = value;
0N/A }
0N/A
0N/A /**
0N/A * Constructs a newly allocated {@code Byte} object that
0N/A * represents the {@code byte} value indicated by the
0N/A * {@code String} parameter. The string is converted to a
0N/A * {@code byte} value in exactly the manner used by the
0N/A * {@code parseByte} method for radix 10.
0N/A *
0N/A * @param s the {@code String} to be converted to a
0N/A * {@code Byte}
0N/A * @throws NumberFormatException If the {@code String}
0N/A * does not contain a parsable {@code byte}.
0N/A * @see java.lang.Byte#parseByte(java.lang.String, int)
0N/A */
0N/A public Byte(String s) throws NumberFormatException {
0N/A this.value = parseByte(s, 10);
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Byte} as a
0N/A * {@code byte}.
0N/A */
0N/A public byte byteValue() {
0N/A return value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Byte} as a
0N/A * {@code short}.
0N/A */
0N/A public short shortValue() {
0N/A return (short)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Byte} as an
0N/A * {@code int}.
0N/A */
0N/A public int intValue() {
0N/A return (int)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Byte} as a
0N/A * {@code long}.
0N/A */
0N/A public long longValue() {
0N/A return (long)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Byte} as a
0N/A * {@code float}.
0N/A */
0N/A public float floatValue() {
0N/A return (float)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns the value of this {@code Byte} as a
0N/A * {@code double}.
0N/A */
0N/A public double doubleValue() {
0N/A return (double)value;
0N/A }
0N/A
0N/A /**
0N/A * Returns a {@code String} object representing this
0N/A * {@code Byte}'s value. The value is converted to signed
0N/A * decimal representation and returned as a string, exactly as if
0N/A * the {@code byte} value were given as an argument to the
0N/A * {@link java.lang.Byte#toString(byte)} method.
0N/A *
0N/A * @return a string representation of the value of this object in
0N/A * base&nbsp;10.
0N/A */
0N/A public String toString() {
1722N/A return Integer.toString((int)value);
0N/A }
0N/A
0N/A /**
1700N/A * Returns a hash code for this {@code Byte}; equal to the result
1700N/A * of invoking {@code intValue()}.
1700N/A *
1700N/A * @return a hash code value for this {@code Byte}
0N/A */
0N/A public int hashCode() {
0N/A return (int)value;
0N/A }
0N/A
0N/A /**
0N/A * Compares this object to the specified object. The result is
0N/A * {@code true} if and only if the argument is not
0N/A * {@code null} and is a {@code Byte} object that
0N/A * contains the same {@code byte} value as this object.
0N/A *
0N/A * @param obj the object to compare with
0N/A * @return {@code true} if the objects are the same;
0N/A * {@code false} otherwise.
0N/A */
0N/A public boolean equals(Object obj) {
0N/A if (obj instanceof Byte) {
0N/A return value == ((Byte)obj).byteValue();
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Compares two {@code Byte} objects numerically.
0N/A *
0N/A * @param anotherByte the {@code Byte} to be compared.
0N/A * @return the value {@code 0} if this {@code Byte} is
0N/A * equal to the argument {@code Byte}; a value less than
0N/A * {@code 0} if this {@code Byte} is numerically less
0N/A * than the argument {@code Byte}; and a value greater than
0N/A * {@code 0} if this {@code Byte} is numerically
0N/A * greater than the argument {@code Byte} (signed
0N/A * comparison).
0N/A * @since 1.2
0N/A */
0N/A public int compareTo(Byte anotherByte) {
1701N/A return compare(this.value, anotherByte.value);
1701N/A }
1701N/A
1701N/A /**
1701N/A * Compares two {@code byte} values numerically.
1701N/A * The value returned is identical to what would be returned by:
1701N/A * <pre>
1701N/A * Byte.valueOf(x).compareTo(Byte.valueOf(y))
1701N/A * </pre>
1701N/A *
1701N/A * @param x the first {@code byte} to compare
1701N/A * @param y the second {@code byte} to compare
1701N/A * @return the value {@code 0} if {@code x == y};
1701N/A * a value less than {@code 0} if {@code x < y}; and
1701N/A * a value greater than {@code 0} if {@code x > y}
1701N/A * @since 1.7
1701N/A */
1701N/A public static int compare(byte x, byte y) {
1701N/A return x - y;
0N/A }
0N/A
0N/A /**
0N/A * The number of bits used to represent a {@code byte} value in two's
0N/A * complement binary form.
0N/A *
0N/A * @since 1.5
0N/A */
0N/A public static final int SIZE = 8;
0N/A
0N/A /** use serialVersionUID from JDK 1.1. for interoperability */
0N/A private static final long serialVersionUID = -7183698231559129828L;
0N/A}