Byte wrapper class is used to wrap primitive data type byte value in an object.
Examples of Byte Wrapper Class
Example 1 : Convert byte to Byte object
public class byteToByte
{
public static void main(String[] args)
{
byte b = 1;
Byte bObj = new Byte(b);
System.out.println(bObj);
}
}
Output
1
This example shows how a byte primitive value can be converted to Byte object.
Example 2 : Convert Byte object to String object
public class ByteToString
{
public static void main(String[] args)
{
Byte bObj = new Byte("1");
String s = bObj.toString();
System.out.println("Converted value of Byte object into string object is " + s);
}
}
Output
Converted value of Byte object into string object is 1
This example shows how a Byte object can be converted into String object.
Example 3 : Convert Byte object to int
public class ByteToint
{
public static void main(String[] args)
{
Byte bObj = new Byte("1");
int i = bObj.intValue();
System.out.println(i);
}
}
Output
1
This example shows how a Byte object can be converted into int.
Example 4 : Convert Byte object to short
public class ByteToshort
{
public static void main(String[] args)
{
Byte bObj = new Byte("1");
short sh = bObj.shortValue();
System.out.println(sh);
}
}
Output
1
This example shows how a Byte object can be converted into short.
Example 5 : Convert Byte object to long
public class ByteTolong
{
public static void main(String[] args)
{
Byte bObj = new Byte("1");
long l = bObj.longValue();
System.out.println(l);
}
}
Output
1
This example shows how a Byte object can be converted into long.
Example 6 : Convert Byte object to float
public class ByteTofloat
{
public static void main(String[] args)
{
Byte bObj = new Byte("1");
float f = bObj.floatValue();
System.out.println(f);
}
}
Output
1.0
This example shows how a Byte object can be converted into float.
Example 7 : Convert Byte object to double
public class ByteTodouble
{
public static void main(String[] args)
{
Byte bObj = new Byte("1");
double d = bObj.doubleValue();
System.out.println(d);
}
}
Output
1.0
This example shows how a Byte object can be converted into double.