Class Byte
- All Implemented Interfaces:
Serializable,Comparable<Byte>,Constable
Byte class is thewrapper class for values of the primitivetypebyte. An object of typeByte contains asingle field whose type isbyte.In addition, this class provides several methods for convertingabyte to aString and aString to abyte, as well as other constants and methods useful when dealingwith abyte.
This is avalue-basedclass; programmers should treat instances that areequal as interchangeable and should notuse instances for synchronization, or unpredictable behavior mayoccur. For example, in a future release, synchronization may fail.
- Since:
- 1.1
- See Also:
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intThe number of bytes used to represent abytevalue in two'scomplement binary form.static final byteA constant holding the maximum value abytecanhave, 27-1.static final byteA constant holding the minimum value abytecanhave, -27.static final intThe number of bits used to represent abytevalue in two'scomplement binary form.TheClassinstance representing the primitive typebyte.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionbyteReturns the value of thisByteas abyte.static intcompare(byte x, byte y) Compares twobytevalues numerically.intCompares twoByteobjects numerically.static intcompareUnsigned(byte x, byte y) Compares twobytevalues numerically treating the valuesas unsigned.staticByteDecodes aStringinto aByte.Returns anOptionalcontaining the nominal descriptor for thisinstance.doubleReturns the value of thisByteas adoubleafter a widening primitive conversion.booleanCompares this object to the specified object.floatReturns the value of thisByteas afloataftera widening primitive conversion.inthashCode()Returns a hash code for thisByte; equal to the resultof invokingintValue().static inthashCode(byte value) Returns a hash code for abytevalue; compatible withByte.hashCode().intintValue()Returns the value of thisByteas anintaftera widening primitive conversion.longReturns the value of thisByteas alongaftera widening primitive conversion.static byteParses the string argument as a signed decimalbyte.static byteParses the string argument as a signedbytein theradix specified by the second argument.shortReturns the value of thisByteas ashortaftera widening primitive conversion.toString()Returns aStringobject representing thisByte's value.staticStringtoString(byte b) Returns a newStringobject representing thespecifiedbyte.static inttoUnsignedInt(byte x) Converts the argument to anintby an unsignedconversion.static longtoUnsignedLong(byte x) Converts the argument to alongby an unsignedconversion.staticBytevalueOf(byte b) Returns aByteinstance representing the specifiedbytevalue.staticByteReturns aByteobject holding the valuegiven by the specifiedString.staticByteReturns aByteobject holding the valueextracted from the specifiedStringwhen parsedwith the radix given by the second argument.
Field Details
MIN_VALUE
public static final byte MIN_VALUEA constant holding the minimum value abytecanhave, -27.- See Also:
MAX_VALUE
public static final byte MAX_VALUEA constant holding the maximum value abytecanhave, 27-1.- See Also:
TYPE
SIZE
public static final int SIZEThe number of bits used to represent abytevalue in two'scomplement binary form.- Since:
- 1.5
- See Also:
BYTES
public static final int BYTESThe number of bytes used to represent abytevalue in two'scomplement binary form.- Since:
- 1.8
- See Also:
Constructor Details
Byte
Deprecated.It is rarely appropriate to use this constructor. The static factoryvalueOf(byte)is generally a better choice, as it islikely to yield significantly better space and time performance.Constructs a newly allocatedByteobject thatrepresents the specifiedbytevalue.- Parameters:
value- the value to be represented by theByte.
Byte
Deprecated.It is rarely appropriate to use this constructor.UseparseByte(String)to convert a string to abyteprimitive, or usevalueOf(String)to convert a string to aByteobject.Constructs a newly allocatedByteobject thatrepresents thebytevalue indicated by theStringparameter. The string is converted to abytevalue in exactly the manner used by theparseBytemethod for radix 10.- Parameters:
s- theStringto be converted to aByte- Throws:
NumberFormatException- if theStringdoes not contain a parsablebyte.
Method Details
toString
Returns a newStringobject representing thespecifiedbyte. The radix is assumed to be 10.- Parameters:
b- thebyteto be converted- Returns:
- the string representation of the specified
byte - See Also:
describeConstable
Returns anOptionalcontaining the nominal descriptor for thisinstance.- Specified by:
describeConstablein interfaceConstable- Returns:
- an
Optionaldescribing theByte instance - Since:
- 15
valueOf
Returns aByteinstance representing the specifiedbytevalue.If a newByteinstance is not required, this methodshould generally be used in preference to the constructorByte(byte), as this method is likely to yieldsignificantly better space and time performance sinceall byte values are cached.- Parameters:
b- a byte value.- Returns:
- a
Byteinstance representingb. - Since:
- 1.5
parseByte
Parses the string argument as a signedbytein theradix specified by the second argument. The characters in thestring must all be digits, of the specified radix (asdetermined by whetherCharacter.digit(char, int)returns a nonnegative value) except that the firstcharacter may be an ASCII minus sign'-'('\u002D') to indicate a negative value or anASCII plus sign'+'('\u002B') toindicate a positive value. The resultingbytevalue isreturned.An exception of type
NumberFormatExceptionisthrown if any of the following situations occurs:- The first argument is
nullor is a string oflength zero. - The radix is either smaller than
Character.MIN_RADIXor larger thanCharacter.MAX_RADIX. - Any character of the string is not a digit of thespecified radix, except that the first character may be a minussign
'-'('\u002D') or plus sign'+'('\u002B') provided that thestring is longer than length 1. - The value represented by the string is not a value of type
byte.
- Parameters:
s- theStringcontaining thebyterepresentation to be parsedradix- the radix to be used while parsings- Returns:
- the
bytevalue represented by the string argument in the specified radix - Throws:
NumberFormatException- If the string does not contain a parsablebyte.
- The first argument is
parseByte
Parses the string argument as a signed decimalbyte. The characters in the string must all be decimal digits,except that the first character may be an ASCII minus sign'-'('\u002D') to indicate a negativevalue or an ASCII plus sign'+'('\u002B') to indicate a positive value. Theresultingbytevalue is returned, exactly as if theargument and the radix 10 were given as arguments to theparseByte(java.lang.String, int)method.- Parameters:
s- aStringcontaining thebyterepresentation to be parsed- Returns:
- the
bytevalue represented by the argument in decimal - Throws:
NumberFormatException- if the string does not contain a parsablebyte.
valueOf
Returns aByteobject holding the valueextracted from the specifiedStringwhen parsedwith the radix given by the second argument. The first argumentis interpreted as representing a signedbyteinthe radix specified by the second argument, exactly as if theargument were given to theparseByte(java.lang.String, int)method. The result is aByteobject thatrepresents thebytevalue specified by the string.In other words, this method returns a
Byteobjectequal to the value of:Byte.valueOf(Byte.parseByte(s, radix))- Parameters:
s- the string to be parsedradix- the radix to be used in interpretings- Returns:
- a
Byteobject holding the value represented by the string argument in the specified radix. - Throws:
NumberFormatException- If theStringdoes not contain a parsablebyte.
valueOf
Returns aByteobject holding the valuegiven by the specifiedString. The argument isinterpreted as representing a signed decimalbyte,exactly as if the argument were given to theparseByte(java.lang.String)method. The result is aByteobject that represents thebytevalue specified by the string.In other words, this method returns a
Byteobjectequal to the value of:Byte.valueOf(Byte.parseByte(s))- Parameters:
s- the string to be parsed- Returns:
- a
Byteobject holding the value represented by the string argument - Throws:
NumberFormatException- If theStringdoes not contain a parsablebyte.
decode
Decodes aStringinto aByte.Accepts decimal, hexadecimal, and octal numbers given bythe following grammar:
DecimalNumeral,HexDigits, andOctalDigitsare as defined in section3.10.1 ofThe Java Language Specification,except that underscores are not accepted between digits.- DecodableString:
- Signopt DecimalNumeral
- Signopt
0xHexDigits- Signopt
0XHexDigits- Signopt
#HexDigits- Signopt
0OctalDigits - Signopt
- Sign:
-+
The sequence of characters following an optionalsign and/or radix specifier ("
0x", "0X","#", or leading zero) is parsed as by theByte.parseBytemethod with the indicated radix (10, 16, or 8).This sequence of characters must represent a positive value oraNumberFormatExceptionwill be thrown. The result isnegated if first character of the specifiedStringisthe minus sign. No whitespace characters are permitted in theString.- Parameters:
nm- theStringto decode.- Returns:
- a
Byteobject holding thebytevalue represented bynm - Throws:
NumberFormatException- if theStringdoes not contain a parsablebyte.- See Also:
byteValue
shortValue
public short shortValue()Returns the value of thisByteas ashortaftera widening primitive conversion.- Overrides:
shortValuein classNumber- Returns:
- the numeric value represented by this object after conversion to type
short. - SeeJava Language Specification:
- 5.1.2 Widening Primitive Conversion
intValue
public int intValue()Returns the value of thisByteas anintaftera widening primitive conversion.- Specified by:
intValuein classNumber- Returns:
- the numeric value represented by this object after conversion to type
int. - SeeJava Language Specification:
- 5.1.2 Widening Primitive Conversion
longValue
public long longValue()Returns the value of thisByteas alongaftera widening primitive conversion.- Specified by:
longValuein classNumber- Returns:
- the numeric value represented by this object after conversion to type
long. - SeeJava Language Specification:
- 5.1.2 Widening Primitive Conversion
floatValue
public float floatValue()Returns the value of thisByteas afloataftera widening primitive conversion.- Specified by:
floatValuein classNumber- Returns:
- the numeric value represented by this object after conversion to type
float. - SeeJava Language Specification:
- 5.1.2 Widening Primitive Conversion
doubleValue
public double doubleValue()Returns the value of thisByteas adoubleafter a widening primitive conversion.- Specified by:
doubleValuein classNumber- Returns:
- the numeric value represented by this object after conversion to type
double. - SeeJava Language Specification:
- 5.1.2 Widening Primitive Conversion
toString
Returns aStringobject representing thisByte's value. The value is converted to signeddecimal representation and returned as a string, exactly as ifthebytevalue were given as an argument to thetoString(byte)method.hashCode
hashCode
public static int hashCode(byte value) Returns a hash code for abytevalue; compatible withByte.hashCode().- Parameters:
value- the value to hash- Returns:
- a hash code value for a
bytevalue. - Since:
- 1.8
equals
Compares this object to the specified object. The result istrueif and only if the argument is notnulland is aByteobject thatcontains the samebytevalue as this object.compareTo
Compares twoByteobjects numerically.- Specified by:
compareToin interfaceComparable<Byte>- Parameters:
anotherByte- theByteto be compared.- Returns:
- the value
0if thisByteis equal to the argumentByte; a value less than0if thisByteis numerically less than the argumentByte; and a value greater than0if thisByteis numerically greater than the argumentByte(signed comparison). - Since:
- 1.2
compare
public static int compare(byte x, byte y) Compares twobytevalues numerically.The value returned is identical to what would be returned by:Byte.valueOf(x).compareTo(Byte.valueOf(y))
- Parameters:
x- the firstbyteto comparey- the secondbyteto compare- Returns:
- the value
0ifx == y; a value less than0ifx < y; and a value greater than0ifx > y - Since:
- 1.7
compareUnsigned
public static int compareUnsigned(byte x, byte y) Compares twobytevalues numerically treating the valuesas unsigned.- Parameters:
x- the firstbyteto comparey- the secondbyteto compare- Returns:
- the value
0ifx == y; a value less than0ifx < yas unsigned values; and a value greater than0ifx > yas unsigned values - Since:
- 9
toUnsignedInt
public static int toUnsignedInt(byte x) Converts the argument to anintby an unsignedconversion. In an unsigned conversion to anint, thehigh-order 24 bits of theintare zero and thelow-order 8 bits are equal to the bits of thebyteargument.Consequently, zero and positivebytevalues are mappedto a numerically equalintvalue and negativebytevalues are mapped to anintvalue equal to theinput plus 28.- Parameters:
x- the value to convert to an unsignedint- Returns:
- the argument converted to
intby an unsigned conversion - Since:
- 1.8
toUnsignedLong
public static long toUnsignedLong(byte x) Converts the argument to alongby an unsignedconversion. In an unsigned conversion to along, thehigh-order 56 bits of thelongare zero and thelow-order 8 bits are equal to the bits of thebyteargument.Consequently, zero and positivebytevalues are mappedto a numerically equallongvalue and negativebytevalues are mapped to alongvalue equal to theinput plus 28.- Parameters:
x- the value to convert to an unsignedlong- Returns:
- the argument converted to
longby an unsigned conversion - Since:
- 1.8