Java Convert boolean to int

Tags: boolean int

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

How to convert boolean to int in Java

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

int intValue = booleanValue ? 1 : 0;

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

BooleanToIntExample1.java

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

        int intValue = booleanValue ? 1 : 0;

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

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

BooleanToIntExample2.java

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

        int intValue = booleanValue ? 1 : 0;

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

Happy Coding 😊

Java Convert boolean to byte

Java Convert boolean to short

Java Convert boolean to long

Java Convert boolean to String