This example shows how to convert Byte object to numeric primitive types byte, short, int, long, float, and double.
How to convert Byte object to numeric primitive types in Java?
Use the xxxValue method of Byte wrapper class to convert Byte object to primitive numeric types where the xxx is a primitive type you want to convert to as given below.
1) Convert Byte to byte
Use the byteValue
method of the Byte wrapper class to convert from Byte to byte primitive value.
1 |
public byte byteValue() |
This method returns the value of the Byte object as a byte primitive.
2) Convert Byte to short
Use the shortValue
method of the Byte wrapper class to convert from Byte to short primitive value.
1 |
public short shortValue() |
This method returns the value of the Byte object as a short primitive.
3) Convert Byte to int
Use the intValue
method of the Byte wrapper class to convert from Byte to int primitive value.
1 |
public int intValue() |
This method returns the value of the Byte object as an int primitive.
4) Convert Byte to long
Use the longValue
method of the Byte wrapper class to convert from Byte to long primitive value.
1 |
public long longValue() |
This method returns the value of the Byte object as a long primitive.
5) Convert Byte to float
Use the floatValue
method of the Byte wrapper class to convert from Byte to float primitive value.
1 |
public float floatValue() |
This method returns the value of the Byte object as a float primitive.
6) Convert Byte to double
Use the doubleValue
method of the Byte wrapper class to convert from Byte to double primitive value.
1 |
public double doubleValue() |
This method returns the value of the Byte object as a double primitive.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javacodeexamples.basic.wrapperclasses; public class ConvertByteToPrimitiveExample { public static void main(String[] args) { Byte b = new Byte("32"); System.out.println("Byte to byte: " + b.byteValue() ); System.out.println("Byte to short: " + b.shortValue() ); System.out.println("Byte to int: " + b.intValue() ); System.out.println("Byte to long: " + b.longValue() ); System.out.println("Byte to float: " + b.floatValue() ); System.out.println("Byte to double: " + b.doubleValue() ); } } |
Output
1 2 3 4 5 6 |
Byte to byte: 32 Byte to short: 32 Byte to int: 32 Byte to long: 32 Byte to float: 32.0 Byte to double: 32.0 |
This example is a part of the Java Basic Examples and Java Type conversion Tutorial.
Please let me know your views in the comments section below.