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