Convert float to byte in Java

Tags: byte float Convert

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

Table of contents

  1. How to cast float to byte value in Java
  2. Using Float.byteValue() method to get byte value from a Float object

How to cast float to byte value in Java

ConvertFloatToByteExample1.java

public class ConvertFloatToByteExample1 {
    public static void main(String[] args) {
        float value1 = 56.78F;
        byte value2 = (byte) value1;

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

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

ConvertFloatToByteExample2.java

public class ConvertFloatToByteExample2 {
    public static void main(String[] args) {
        Float value1 = 23.45F;
        byte value2 = value1.byteValue();

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

Happy Coding 😊

Convert byte to float in Java

Convert float to String in Java

Convert float to long in Java

Convert float to short in Java

Convert float to int in Java

Convert float to double in Java