Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/lang/Byte.java
38829 views
/*1* Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.lang;2627/**28*29* The {@code Byte} class wraps a value of primitive type {@code byte}30* in an object. An object of type {@code Byte} contains a single31* field whose type is {@code byte}.32*33* <p>In addition, this class provides several methods for converting34* a {@code byte} to a {@code String} and a {@code String} to a {@code35* byte}, as well as other constants and methods useful when dealing36* with a {@code byte}.37*38* @author Nakul Saraiya39* @author Joseph D. Darcy40* @see java.lang.Number41* @since JDK1.142*/43public final class Byte extends Number implements Comparable<Byte> {4445/**46* A constant holding the minimum value a {@code byte} can47* have, -2<sup>7</sup>.48*/49public static final byte MIN_VALUE = -128;5051/**52* A constant holding the maximum value a {@code byte} can53* have, 2<sup>7</sup>-1.54*/55public static final byte MAX_VALUE = 127;5657/**58* The {@code Class} instance representing the primitive type59* {@code byte}.60*/61@SuppressWarnings("unchecked")62public static final Class<Byte> TYPE = (Class<Byte>) Class.getPrimitiveClass("byte");6364/**65* Returns a new {@code String} object representing the66* specified {@code byte}. The radix is assumed to be 10.67*68* @param b the {@code byte} to be converted69* @return the string representation of the specified {@code byte}70* @see java.lang.Integer#toString(int)71*/72public static String toString(byte b) {73return Integer.toString((int)b, 10);74}7576private static class ByteCache {77private ByteCache(){}7879static final Byte cache[] = new Byte[-(-128) + 127 + 1];8081static {82for(int i = 0; i < cache.length; i++)83cache[i] = new Byte((byte)(i - 128));84}85}8687/**88* Returns a {@code Byte} instance representing the specified89* {@code byte} value.90* If a new {@code Byte} instance is not required, this method91* should generally be used in preference to the constructor92* {@link #Byte(byte)}, as this method is likely to yield93* significantly better space and time performance since94* all byte values are cached.95*96* @param b a byte value.97* @return a {@code Byte} instance representing {@code b}.98* @since 1.599*/100public static Byte valueOf(byte b) {101final int offset = 128;102return ByteCache.cache[(int)b + offset];103}104105/**106* Parses the string argument as a signed {@code byte} in the107* radix specified by the second argument. The characters in the108* string must all be digits, of the specified radix (as109* determined by whether {@link java.lang.Character#digit(char,110* int)} returns a nonnegative value) except that the first111* character may be an ASCII minus sign {@code '-'}112* ({@code '\u005Cu002D'}) to indicate a negative value or an113* ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to114* indicate a positive value. The resulting {@code byte} value is115* returned.116*117* <p>An exception of type {@code NumberFormatException} is118* thrown if any of the following situations occurs:119* <ul>120* <li> The first argument is {@code null} or is a string of121* length zero.122*123* <li> The radix is either smaller than {@link124* java.lang.Character#MIN_RADIX} or larger than {@link125* java.lang.Character#MAX_RADIX}.126*127* <li> Any character of the string is not a digit of the128* specified radix, except that the first character may be a minus129* sign {@code '-'} ({@code '\u005Cu002D'}) or plus sign130* {@code '+'} ({@code '\u005Cu002B'}) provided that the131* string is longer than length 1.132*133* <li> The value represented by the string is not a value of type134* {@code byte}.135* </ul>136*137* @param s the {@code String} containing the138* {@code byte}139* representation to be parsed140* @param radix the radix to be used while parsing {@code s}141* @return the {@code byte} value represented by the string142* argument in the specified radix143* @throws NumberFormatException If the string does144* not contain a parsable {@code byte}.145*/146public static byte parseByte(String s, int radix)147throws NumberFormatException {148int i = Integer.parseInt(s, radix);149if (i < MIN_VALUE || i > MAX_VALUE)150throw new NumberFormatException(151"Value out of range. Value:\"" + s + "\" Radix:" + radix);152return (byte)i;153}154155/**156* Parses the string argument as a signed decimal {@code157* byte}. The characters in the string must all be decimal digits,158* except that the first character may be an ASCII minus sign159* {@code '-'} ({@code '\u005Cu002D'}) to indicate a negative160* value or an ASCII plus sign {@code '+'}161* ({@code '\u005Cu002B'}) to indicate a positive value. The162* resulting {@code byte} value is returned, exactly as if the163* argument and the radix 10 were given as arguments to the {@link164* #parseByte(java.lang.String, int)} method.165*166* @param s a {@code String} containing the167* {@code byte} representation to be parsed168* @return the {@code byte} value represented by the169* argument in decimal170* @throws NumberFormatException if the string does not171* contain a parsable {@code byte}.172*/173public static byte parseByte(String s) throws NumberFormatException {174return parseByte(s, 10);175}176177/**178* Returns a {@code Byte} object holding the value179* extracted from the specified {@code String} when parsed180* with the radix given by the second argument. The first argument181* is interpreted as representing a signed {@code byte} in182* the radix specified by the second argument, exactly as if the183* argument were given to the {@link #parseByte(java.lang.String,184* int)} method. The result is a {@code Byte} object that185* represents the {@code byte} value specified by the string.186*187* <p> In other words, this method returns a {@code Byte} object188* equal to the value of:189*190* <blockquote>191* {@code new Byte(Byte.parseByte(s, radix))}192* </blockquote>193*194* @param s the string to be parsed195* @param radix the radix to be used in interpreting {@code s}196* @return a {@code Byte} object holding the value197* represented by the string argument in the198* specified radix.199* @throws NumberFormatException If the {@code String} does200* not contain a parsable {@code byte}.201*/202public static Byte valueOf(String s, int radix)203throws NumberFormatException {204return valueOf(parseByte(s, radix));205}206207/**208* Returns a {@code Byte} object holding the value209* given by the specified {@code String}. The argument is210* interpreted as representing a signed decimal {@code byte},211* exactly as if the argument were given to the {@link212* #parseByte(java.lang.String)} method. The result is a213* {@code Byte} object that represents the {@code byte}214* value specified by the string.215*216* <p> In other words, this method returns a {@code Byte} object217* equal to the value of:218*219* <blockquote>220* {@code new Byte(Byte.parseByte(s))}221* </blockquote>222*223* @param s the string to be parsed224* @return a {@code Byte} object holding the value225* represented by the string argument226* @throws NumberFormatException If the {@code String} does227* not contain a parsable {@code byte}.228*/229public static Byte valueOf(String s) throws NumberFormatException {230return valueOf(s, 10);231}232233/**234* Decodes a {@code String} into a {@code Byte}.235* Accepts decimal, hexadecimal, and octal numbers given by236* the following grammar:237*238* <blockquote>239* <dl>240* <dt><i>DecodableString:</i>241* <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>242* <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>243* <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>244* <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>245* <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>246*247* <dt><i>Sign:</i>248* <dd>{@code -}249* <dd>{@code +}250* </dl>251* </blockquote>252*253* <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>254* are as defined in section 3.10.1 of255* <cite>The Java™ Language Specification</cite>,256* except that underscores are not accepted between digits.257*258* <p>The sequence of characters following an optional259* sign and/or radix specifier ("{@code 0x}", "{@code 0X}",260* "{@code #}", or leading zero) is parsed as by the {@code261* Byte.parseByte} method with the indicated radix (10, 16, or 8).262* This sequence of characters must represent a positive value or263* a {@link NumberFormatException} will be thrown. The result is264* negated if first character of the specified {@code String} is265* the minus sign. No whitespace characters are permitted in the266* {@code String}.267*268* @param nm the {@code String} to decode.269* @return a {@code Byte} object holding the {@code byte}270* value represented by {@code nm}271* @throws NumberFormatException if the {@code String} does not272* contain a parsable {@code byte}.273* @see java.lang.Byte#parseByte(java.lang.String, int)274*/275public static Byte decode(String nm) throws NumberFormatException {276int i = Integer.decode(nm);277if (i < MIN_VALUE || i > MAX_VALUE)278throw new NumberFormatException(279"Value " + i + " out of range from input " + nm);280return valueOf((byte)i);281}282283/**284* The value of the {@code Byte}.285*286* @serial287*/288private final byte value;289290/**291* Constructs a newly allocated {@code Byte} object that292* represents the specified {@code byte} value.293*294* @param value the value to be represented by the295* {@code Byte}.296*/297public Byte(byte value) {298this.value = value;299}300301/**302* Constructs a newly allocated {@code Byte} object that303* represents the {@code byte} value indicated by the304* {@code String} parameter. The string is converted to a305* {@code byte} value in exactly the manner used by the306* {@code parseByte} method for radix 10.307*308* @param s the {@code String} to be converted to a309* {@code Byte}310* @throws NumberFormatException If the {@code String}311* does not contain a parsable {@code byte}.312* @see java.lang.Byte#parseByte(java.lang.String, int)313*/314public Byte(String s) throws NumberFormatException {315this.value = parseByte(s, 10);316}317318/**319* Returns the value of this {@code Byte} as a320* {@code byte}.321*/322public byte byteValue() {323return value;324}325326/**327* Returns the value of this {@code Byte} as a {@code short} after328* a widening primitive conversion.329* @jls 5.1.2 Widening Primitive Conversions330*/331public short shortValue() {332return (short)value;333}334335/**336* Returns the value of this {@code Byte} as an {@code int} after337* a widening primitive conversion.338* @jls 5.1.2 Widening Primitive Conversions339*/340public int intValue() {341return (int)value;342}343344/**345* Returns the value of this {@code Byte} as a {@code long} after346* a widening primitive conversion.347* @jls 5.1.2 Widening Primitive Conversions348*/349public long longValue() {350return (long)value;351}352353/**354* Returns the value of this {@code Byte} as a {@code float} after355* a widening primitive conversion.356* @jls 5.1.2 Widening Primitive Conversions357*/358public float floatValue() {359return (float)value;360}361362/**363* Returns the value of this {@code Byte} as a {@code double}364* after a widening primitive conversion.365* @jls 5.1.2 Widening Primitive Conversions366*/367public double doubleValue() {368return (double)value;369}370371/**372* Returns a {@code String} object representing this373* {@code Byte}'s value. The value is converted to signed374* decimal representation and returned as a string, exactly as if375* the {@code byte} value were given as an argument to the376* {@link java.lang.Byte#toString(byte)} method.377*378* @return a string representation of the value of this object in379* base 10.380*/381public String toString() {382return Integer.toString((int)value);383}384385/**386* Returns a hash code for this {@code Byte}; equal to the result387* of invoking {@code intValue()}.388*389* @return a hash code value for this {@code Byte}390*/391@Override392public int hashCode() {393return Byte.hashCode(value);394}395396/**397* Returns a hash code for a {@code byte} value; compatible with398* {@code Byte.hashCode()}.399*400* @param value the value to hash401* @return a hash code value for a {@code byte} value.402* @since 1.8403*/404public static int hashCode(byte value) {405return (int)value;406}407408/**409* Compares this object to the specified object. The result is410* {@code true} if and only if the argument is not411* {@code null} and is a {@code Byte} object that412* contains the same {@code byte} value as this object.413*414* @param obj the object to compare with415* @return {@code true} if the objects are the same;416* {@code false} otherwise.417*/418public boolean equals(Object obj) {419if (obj instanceof Byte) {420return value == ((Byte)obj).byteValue();421}422return false;423}424425/**426* Compares two {@code Byte} objects numerically.427*428* @param anotherByte the {@code Byte} to be compared.429* @return the value {@code 0} if this {@code Byte} is430* equal to the argument {@code Byte}; a value less than431* {@code 0} if this {@code Byte} is numerically less432* than the argument {@code Byte}; and a value greater than433* {@code 0} if this {@code Byte} is numerically434* greater than the argument {@code Byte} (signed435* comparison).436* @since 1.2437*/438public int compareTo(Byte anotherByte) {439return compare(this.value, anotherByte.value);440}441442/**443* Compares two {@code byte} values numerically.444* The value returned is identical to what would be returned by:445* <pre>446* Byte.valueOf(x).compareTo(Byte.valueOf(y))447* </pre>448*449* @param x the first {@code byte} to compare450* @param y the second {@code byte} to compare451* @return the value {@code 0} if {@code x == y};452* a value less than {@code 0} if {@code x < y}; and453* a value greater than {@code 0} if {@code x > y}454* @since 1.7455*/456public static int compare(byte x, byte y) {457return x - y;458}459460/**461* Converts the argument to an {@code int} by an unsigned462* conversion. In an unsigned conversion to an {@code int}, the463* high-order 24 bits of the {@code int} are zero and the464* low-order 8 bits are equal to the bits of the {@code byte} argument.465*466* Consequently, zero and positive {@code byte} values are mapped467* to a numerically equal {@code int} value and negative {@code468* byte} values are mapped to an {@code int} value equal to the469* input plus 2<sup>8</sup>.470*471* @param x the value to convert to an unsigned {@code int}472* @return the argument converted to {@code int} by an unsigned473* conversion474* @since 1.8475*/476public static int toUnsignedInt(byte x) {477return ((int) x) & 0xff;478}479480/**481* Converts the argument to a {@code long} by an unsigned482* conversion. In an unsigned conversion to a {@code long}, the483* high-order 56 bits of the {@code long} are zero and the484* low-order 8 bits are equal to the bits of the {@code byte} argument.485*486* Consequently, zero and positive {@code byte} values are mapped487* to a numerically equal {@code long} value and negative {@code488* byte} values are mapped to a {@code long} value equal to the489* input plus 2<sup>8</sup>.490*491* @param x the value to convert to an unsigned {@code long}492* @return the argument converted to {@code long} by an unsigned493* conversion494* @since 1.8495*/496public static long toUnsignedLong(byte x) {497return ((long) x) & 0xffL;498}499500501/**502* The number of bits used to represent a {@code byte} value in two's503* complement binary form.504*505* @since 1.5506*/507public static final int SIZE = 8;508509/**510* The number of bytes used to represent a {@code byte} value in two's511* complement binary form.512*513* @since 1.8514*/515public static final int BYTES = SIZE / Byte.SIZE;516517/** use serialVersionUID from JDK 1.1. for interoperability */518private static final long serialVersionUID = -7183698231559129828L;519}520521522