Class Byte in Java
The Byte class provides an object wrapper for a byte value. This is useful when we need to treat a byte value as an object. For example, there are a number of utility methods that take a reference to an Object as one of their arguments. We cannot specify a byte value for one of these arguments, but we can provide a reference to a Byte object that encapsulates the byte value. Furthermore, the Byte class is necessary to support the Reflection API and class literals.
The Byte class also provides a number of utility methods for converting byte values to other primitive types and for converting byte values to strings and vice versa.
The structure of the class Byte is given by
public final class java.lang.Byte extends java.lang.Number {
// Constants
public static final byte MAX_VALUE;
public static final byte MIN_VALUE;
public static final Class TYPE;
// Constructors
public Byte(byte value);
public Byte(String s);
// Class Methods
public static Byte decode(String nm);
public static byte parseByte(String s);
public static byte parseByte(String s, int radix);
public static String toString(byte b);
public static Byte valueOf(String s, int radix);
public static Byte valueOf(String s);
// Instance Methods
public byte byteValue();
public double doubleValue;
public boolean equals(Object obj);
public float floatValue
public int hashCode();
public int intValue();
public long longValue();
public short shortValue();
public String toString();
}
The details of the class structure are given as follows:
public static final byte MAX_VALUE;
public static final byte MAX_VALUE is a constant that represents the largest value that can be represented by a byte. like-MAX_VALUE= 127
public static final byte MIN_VALUE;
public static final byte MIN_VALUE is a constant that represents the smallest value that can be represented by a byte. like MIN_VALUE= -128
public static final Class TYPE;
public static final Class TYPE is a constant that represents the Class object that represents the primitive type byte. It is always true that Byte.TYPE == byte.class.
public Byte(byte value);
public Byte(byte value) constructs a Byte object with the specified byte value.
Parameter
value – The byte value to be encapsulated by this object.
public Byte(String s);
public Byte(String s) method constructs a Byte object with the value specified by the given string. The string should consist of one or more digit characters. The digit characters can be preceded by a single ‘-‘ character. If the string contains any other characters, the constructor throws a NumberFormatException.
Parameter
s – The string to be made into a Byte object.
public static Byte decode(String nm);
public static Byte decode(String nm) method returns a Byte object that encapsulates the given value.
This method returns a Byte object that encapsulates the given value.
Parameter
nm – A String representation of the value to be encapsulated by a Byte object. If the string begins with # or 0x, it is a radix 16 representation of the value. If the string begins with 0, it is a radix 8 representation of the value. Otherwise, it is assumed to be a radix 10 representation of the value.
public static byte parseByte(String s);
public static byte parseByte(String s) method returns the numeric value of the byte represented by the contents of the given String object. The String must contain only decimal digits, except that the first character may be a minus sign.
This method returns the numeric value of the byte represented by the String object.
Parameter
s – The String to be converted to a byte value.
public static byte parseByte(String s, int radix);
public static byte parseByte(String s, int radix) method returns the numeric value of the byte represented by the contents of the given String object in the specified radix. The String must contain only valid digits of the specified radix, except that the first character may be a minus sign. The digits are parsed in the specified radix to produce the numeric value.
This method returns the numeric value of the byte represented by the String object in the specified radix.
Parameter
s – The String to be converted to a byte value.
radix – The radix used in interpreting the characters in the String as digits. This value must be in the range Character.MIN_RADIX to Character.MAX_RADIX. If radix is in the range 2 through 10, only characters for which the Character.isDigit() method returns true are considered to be valid digits. If radix is in the range 11 through 36, characters in the ranges ‘A’ through ‘Z’ and ‘a’ through ‘z’ may be considered valid digits.
public static String toString(byte b);
public static String toString(byte b) method returns a String object that contains the decimal representation of the given value. That is a string that begins with ‘-‘ if the given value is negative. The rest of the string is a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, and ‘9’. This method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with “0” or “-0”.
This method returns the string representation of the given value.
Parameter
b – The byte value to be converted to a string.
public static Byte valueOf(String s, int radix);
public static Byte valueOf(String s, int radix) method constructs a Byte object with the value specified by the given string in the specified radix. The string should consist of one or more digit characters or characters in the range ‘A’ to ‘Z’ or ‘a’ to ‘z’ that are considered digits in the given radix. The digit characters can be preceded by a single ‘-‘ character. If the string contains any other characters, the method throws a NumberFormatException.
This method returns the Byte object constructed from the string.
Parameter
s – The string to be made into a Byte object.
radix – The radix used in converting the string to a value. This value must be in the range Character.MIN_RADIX to Character.MAX_RADIX.
public static Byte valueOf(String s);
public static Byte valueOf(String s) method constructs a Byte object with the value specified by the given string. The string should consist of one or more digit characters. The digit characters can be preceded by a single ‘-‘ character. If the string contains any other characters, the method throws a NumberFormatException.
This method returns the Byte object constructed from the string.
Parameter
s – The string to be made into a Byte object.
public byte byteValue();
public byte byteValue() method returns the value of this object as a byte.
This method returns the value of this object as a byte.
public double doubleValue;
public double doubleValue method returns the value of this object as a double.
This method returns the value of this object as a double.
public boolean equals(Object obj);
public boolean equals(Object obj) method returns true if obj is an instance of Byte and it contains the same value as the object this method is associated with.
This method returns true if the objects are equal; false if they are not.
Parameter
obj – The object to be compared with this object.
public float floatValue
public float floatValu method returns the value of this object as a float.
This method returns the value of this object as a float.
public int hashCode();
public int hashCode() method returns a hashcode computed from the value of this object.
This method returns a hashcode based on the byte value of the object.
public int intValue();
public int intValue() method returns the value of this object as an int.
This method returns the value of this object as an int.
public long longValue();
public long longValue() method returns the value of this object as a long.
This method returns the value of this object as a long
public short shortValue();
public short shortValue() method returns the value of this object as a short.
This method returns the value of this object as a short.
public String toString();
public String toString() method returns a String object that contains the decimal representation of the value of this object. The string begins with ‘-‘ if the given value is negative. The rest of the string is a sequence of one or more of the characters ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, and ‘9’. This method returns “0” if its argument is 0. Otherwise, the string returned by this method does not begin with “0” or “-0”.
This method returns the string representation of the value of this object.
Apart from these Byte class also has inherited methods from class- Object. They are as follows:
- clone()
- finalize()
- notifyAll()
- wait()
- wait(long, int)
- getClass()
- notify()
- wait(long)