Byte and Short Wrapper classes in Java
|We have already explored the abstract class Number in the previous tutorial, which defines the super-class implemented by classes that wrap or encapsulate the numeric types like byte, short, int, long float, and double. In this tutorial, we shall work with the smaller non-floating point numeric type wrappers, i.e. Byte and Short.
Byte Wrapper Class
The Byte wrapper class is used to wrap the values of the byte type. The Byte constructor takes a byte number as an argument or a String that contains a whole numeric value. The constructor for the Byte class is shown here:
Byte(byte num) Byte(String str)throws NumberFormatException
The construction of a Byte type object is shown below:
Byte b = new Byte("100");
Note: If we pass 100 directly, Java takes it as an integer literal and does not allow it to be passed as there is no byte literal.
Methods defined by Byte Wrapper class
METHODS | DESCRIPTION |
---|---|
byte byteValue() | Returns the value of the invoking object as a byte. |
static int compare(byte num1,byte num2) | Compares the value of num1 and num2 and returns 0 if equal. Returns negative value if num1 is less than num2 and returns a positive value otherwise. |
int compareTo(Byte b) | Compares the numerical value of the invoking object with b and returns 0 if equal. Returns a negative value if invoking object has a lower value and returns a positive value otherwise. |
static Byte decode(String str)throws NumberFormatException | returns a Byte object that contains the value stored in the String str. |
double doubleValue() | Returns the value of the invoking object as double. |
boolean equals(Object ByteObj) | Returns true is invoking Byte object is equivalent to ByteObj otherwise false. |
float floatValue() | Returns the value of the invoking object as a float. |
int hashCode() | Returns the hash code for the invoking object. |
static int hashCode(byte num) | Returns the hash code for num. It was added by JDK8. |
int intValue() | Returns the value of the invoking object as an int. |
long longValue() | Returns the value of the invoking object as long. |
static byte parseByte(String str)throws NumberFormatException | Returns the byte equivalent of the number in str using radix 10. |
static byte parseByte(String str, int radix)throws NumberFormatException | Returns the byte equivalent of the number in str using the radix specified. |
short shortValue() | Returns the value of the invoking object as short. |
String toString() | Returns a String value which contains the decimal equivalent of the invoking object. |
static String toString(byte num) | Returns a String value which contains the decimal equivalent of num. |
static int toUnsignedInt(byte num) | Returns the value of num as an unsigned integer. It was added by JDK8. |
static long toUnsignedLong(byte num) | Returns the value of num as an unsigned long integer. It was added by JDK8. |
static Byte valueOf(byte num) | Returns a Byte object that contains the value stored in num. |
static Byte valueOf(String str)throws NumberFormatException | Returns a Byte object that contains the value stored in str using radix 10. |
static Byte valueOf(String str, int radix)throws NumberFormatException | Returns a Byte object that contains the value stored in str using the specified radix. |
Short Wrapper Class
The Short wrapper class is used to wrap the values of the short type. The Short constructor takes a short number as an argument or a String that contains a whole numeric value. The constructor for the Short class is shown here:
Short(short num) Short(String str)throws NumberFormatException
The construction of a Short type object is shown below:
Short s = new Short(16);
Note: If we pass 16 directly, Java takes it as an integer literal and does not allow it to be passed as there is no short literal.
Methods defined by Short Wrapper class
METHODS | DESCRIPTION |
---|---|
byte byteValue() | Returns the value of the invoking object as a byte. |
static int compare(short num1, short num2) | Compares the value of num1 and num2 and returns 0 if equal. Returns negative value if num1 is less than num2 and returns a positive value otherwise. |
int compareTo(Short s) | Compares the numerical value of the invoking object with s and returns 0 if equal. Returns a negative value if invoking object has a lower value and returns a positive value otherwise. |
static Short decode(String str)throws NumberFormatException | Returns a Short object that contains the value stored in the String str. |
double doubleValue() | Returns the value of the invoking object as double. |
boolean equals(Object ShortObj) | Returns true is invoking Short object is equivalent to ShortObj otherwise false. |
float floatValue() | Returns the value of the invoking object as a float. |
int hashCode() | Returns the hash code for the invoking object. |
static int hashCode(short num) | Returns the hash code for num. It was added by JDK8. |
int intValue() | Returns the value of the invoking object as an int. |
long longValue() | Returns the value of the invoking object as long. |
static short parseShort(String str)throws NumberFormatException | Returns the short equivalent of the number in str using radix 10. |
static short parseShort(String str, int radix)throws NumberFormatException | Returns the short equivalent of the number in str using the radix specified. |
static short reverseBytes(Short num) | Exchanges the high- and low-order bytes of num and returns the result. |
short shortValue() | Returns the value of the invoking object as short. |
String toString() | Returns a String value which contains the decimal equivalent of the invoking object. |
static String toString(short num) | Returns a String value which contains the decimal equivalent of num. |
static int toUnsignedInt(short num) | Returns the value of num as an unsigned integer. It was added by JDK8. |
static long toUnsignedLong(short num) | Returns the value of num as an unsigned long integer. It was added by JDK8. |
static Short valueOf(short num) | Returns a Short object that contains the value stored in num. |
static Short valueOf(String str)throws NumberFormatException | Returns a Short object that contains the value stored in str using radix 10. |
static Short valueOf(String str, int radix)throws NumberFormatException | Returns a Short object that contains the value stored in str using the specified radix. |
Program to show the use of Byte and Short Wrapper classes
/** * This program creates one Byte object and one Short object. * It displays the use of the function doubleValue() and reverseBytes() */ public class ByteShortDemo { public static void main(String args[]) { //Wrapping the byte number into Byte Object. Byte b = new Byte("100"); //Wrapping the short number into Short Object. Short s = new Short("16"); //Displaying the values as well as the double form of b and the reversed byte of s System.out.println("The double value of " + b + " is " + b.doubleValue()); System.out.println("Reversed byte of " + s + " is " + Short.reverseBytes(s)); } }
Output:-
The double value of 100 is 100.0
Reversed byte of 16 is 4096
Knowledge is most useful when liberated and shared. Share this to motivate us to keep writing such online tutorials for free and do comment if anything is missing or wrong or you need any kind of help.
Do not forget to share and Subscribe.
Keep Learning… Happy Learning.. 🙂