Convert byte to float in Java

Tags: byte float Convert

In this Java core tutorial, we learn how to convert byte value into float value in Java via different solutions.

Assign byte variable to float variable

ConvertByteToFloatExample1.java

public class ConvertByteToFloatExample1 {
    public static void main(String[] args) {
        byte value1 = 10;
        float value2 = value1;

        System.out.println("byte value: " + value1);
        System.out.println("float value: " + value2);
    }
}
The output is:
byte value: 10
float value: 10.0

Using Float.valueOf() method to instantiate float from a byte value

ConvertByteToFloatExample2.java

public class ConvertByteToFloatExample2 {
    public static void main(String[] args) {
        byte value1 = 99;
        float value2 = Float.valueOf(value1);

        System.out.println("byte value: " + value1);
        System.out.println("float value: " + value2);
    }
}
The output is:
byte value: 99
float value: 99.0

Using Byte.floatValue() method to get byte value from a Float object

ConvertByteToFloatExample3.java

public class ConvertByteToFloatExample3 {
    public static void main(String[] args) {
        Byte value1 = Byte.valueOf((byte) 19);

        float value2 = value1.floatValue();

        System.out.println("byte value: " + value1);
        System.out.println("float value: " + value2);
    }
}
The output is:
byte value: 19
float value: 19.0

Happy Coding 😊