Advertisement



< Prev
Next >



Byte Wrapper Class





Byte wrapper class is used to create an object version of a primitive byte value.





class A
{
public static void main(String... ar)
{

//In Java, by default any numeric non-floating value is of primitive int type, so its casting to byte will be required 
//it is sent to the constructor of the Byte wrapper class.

Byte b1 = new Byte((byte)10); //Boxing primitive byte in a wrapper Byte object.

System.out.println(b1);	//Unboxing the Byte object.


//Reading primitive byte out of Byte object using byteValue() method
Byte b2 = new Byte("10"); //Passing primitive byte as a String
System.out.println("b2.byteValue());
}
}


Output-


A
A



Advertisement




Some important methods of Byte wrapper class


Methods Description
int compareTo(Byte b) - Returns a zero if the invoked Byte object contains the same byte value as b.
- Returns a positive value if the invoked Byte object contains greater value than b.
- Returns a negative value if the invoked Byte object contains smaller value than b.
boolean equals(Object ob) Returns a true if the invoked Byte object has same value as referred by ob, else false.
static byte parseByte(String s) Returns a primitive byte value if String s could be converted to a valid byte value.
static Byte valueOf(byte b) Returns a Byte object after converting it from primitive byte value, b.
short shortValue() Returns an primitive short value after converting it from an invoked Byte object.
byte byteValue() Returns an byte value after converting it from an invoked Byte object.
int intValue() Returns an int value after converting it from an invoked Byte object.
long longValue() Returns an long value after converting it from an invoked Byte object.
float floatValue() Returns an float value after converting it from an invoked Byte object.
double doubleValue() Returns an double value after converting it from an invoked Byte object.





Using compareTo() method to compare values in two Byte objects.


Method compareTo() takes a Byte class type object and it -
class A
{
public static void main(String... ar)
{
Byte b1 = new Byte("10");       //Constructor accepting String value
Byte b2 = new Byte((byte)10);	  //casting an int to byte to pass to Constructor of Byte class.

System.out.println("Value in b1 = "+b1);
System.out.println("Value in b2 = "+b2);

System.out.println("Invoking b1 to compare with b2 : "+ b1.compareTo(b2));

Byte b3 = new Byte("20");
Byte b4 = new Byte((byte)25);

System.out.println("Value in b3 = "+b3);
System.out.println("Value in b4 = "+b4);

System.out.println("Invoking b3 to compare with b4 : "+ b3.compareTo(b4));

System.out.println("Invoking b4 to compare with b3 : "+ b4.compareTo(b3));
}
}


Output-


Value in b1 = 10
Value in b2 = 10
Invoking b1 to compare with b2 : 0
Value in b3 = 20
Value in b4 = 25
Invoking b3 to compare with b4 : -5
Invoking b4 to compare with b3 : 5





To convert a String to a primitive byte value using parseByte() method.


Method parseByte(), converts a string value which could be parse to a primitive byte value.
//Converting String to primitive byte value.

class A
{
public static void main(String... ar)
{
byte b1 = Byte.parseByte("10");
byte b2 = Byte.parseByte("100");
byte b3 = Byte.parseByte("50");

System.out.println("Primitive byte value in b1 : "+ b1);
System.out.println("Primitive byte value in b2 : "+ b2);
System.out.println("Primitive byte value in b3 : "+ b3);
}
}


Output-


Primitive byte value in b1 : 10
Primitive byte value in b2 : 100
Primitive byte value in b3 : 50





Exception when converting a String to Byte


Exception could be caused in the following situations -

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement