Java Byte Class
The Byte class is a wrapper class which is used to wraps a value of primitive type byte in an object. An object of type Byte contains a single field whose type is byte.
This class is located into java.lang package and provides several methods for converting a byte to a String and vice versa. The declaration of this class is given below.
Declaration:
Now lets see its methods and their usage with the help of examples.
1. toString()
Syntax:
Example:
Lets take an example to get string object of a byte type. We used toString()
method which is static so we can call it by using the class name.
Â
2. valueOf()
This method returns a Byte instance representing the specified byte value. This method should generally be used in preference to the constructor Byte(byte).
Syntax:
Example:
In this example, we are using valueOf()
method that returns instance of Byte class which represents the specified byte type.
Â
3. parseByte()
This method returns a byte value of the specified string val. We can use it to get a byte value from string type value.
Example:
Lets take an example in which we have a string type variable and getting its byte value using the parseByte() method.
Â
4. decode()
This method is used to decode a String into a Byte. It accepts decimal, hexadecimal, and octal numbers.
Syntax:
Example:
We can use decode method to decode string type to a byte object. See the below example.
Â
5. byteValue()
This method is used to get a primitive type byte value from Byte object. It returns the numeric value represented by this object after conversion to type byte.
Syntax
6. shortValue()
This method returns the value of this Byte as a short after a widening primitive conversion.
Syntax
7. intValue()
The intValue()
method returns the value of this Byte as a primitive int type after a widening primitive conversion.
Syntax
8. longValue()
The longValue()
method returns the value of this Byte type as a long type after a widening primitive conversion.
Syntax
9. doubleValue()
It returns the value of this Byte type as a double type after a widening primitive conversion.
Syntax
10. floatValue()
This method is used to get value of this Byte type as a float type after a widening primitive conversion.
Syntax:
Example:
Lets take an example to convert byte type to int, long and float type values. In this example, we are using intValue(), floatValue(), doubleValue()
methods.
Â
11. hashCode()
This method is used to get hash code of a byte value. It returns an int value of byte object.
Syntax:
Example:
Â
12. equals()
The equals()
method compares an object to the specified object. It returns true if objects are same; false otherwise.
Syntax:
Example:
We are comparing two byte objects using the equals method that returns true if both the objects are true.
Â
13. compareTo()
This method is used to compare two byte objects numerically. It returns 0, if the both byte objects are equal. It returns less the 0, if one byte object is less than argument object. It returns greater than 0, if one byte object is numerically greater that the argument byte object.
Syntax:
public int compareTo(Byte anotherByte)
Example:
In this example, we are comparing two bytes objects using compareTo()
method that compares two byte objects numerically and returns a numeric value.
Â
14. compare()
It is used to compare two byte values numerically. The value returned is identical to what would be returned by.
Syntax:
Example:
We can use compare method to compare two byte values. It returns 0 if both are equal else returns either negative or positive numerical value.
Â
Â