Java Convert boolean to long

Tags: boolean long

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

How to convert boolean to long in Java

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

long longValue = booleanValue ? 1 : 0;

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

BooleanToLongExample1.java

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

        long longValue = booleanValue ? 1 : 0;

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

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

BooleanToLongExample2.java

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

        long longValue = booleanValue ? 1 : 0;

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

Happy Coding 😊

Java Convert boolean to int

Java Convert boolean to byte

Java Convert boolean to short

Java Convert boolean to String