Java Convert boolean to byte

Tags: boolean byte

In this Java core tutorial we learn how to convert a boolean value into byte value in Java programming language.

How to convert boolean to byte in Java

To convert an boolean value to byte we simply map the boolean true value to 1 in byte and false value to 0 in byte.

byte byteValue = (byte)(booleanValue ? 1 : 0);

The following Java example code we convert a boolean true value to byte.

BooleanToByteExample1.java

public class BooleanToByteExample1 {
    public static void main(String... args) {
        boolean booleanValue = true;

        byte byteValue = (byte)(booleanValue ? 1 : 0);

        System.out.println("Boolean value: " + booleanValue);
        System.out.println("Byte value: " + byteValue);
    }
}
The output as below.
Boolean value: true
Byte value: 1

In the following Java example code we convert a boolean false value to byte.

BooleanToByteExample2.java

public class BooleanToByteExample2 {
    public static void main(String... args) {
        boolean booleanValue = false;

        byte byteValue = (byte)(booleanValue ? 1 : 0);

        System.out.println("Boolean value: " + booleanValue);
        System.out.println("Byte value: " + byteValue);
    }
}
The output as below.
Boolean value: false
Byte value: 0

Happy Coding 😊

Java Convert boolean to int

Java Convert boolean to short

Java Convert boolean to long

Java Convert boolean to String