Java Program to convert boolean to integer

Java 8Object Oriented ProgrammingProgramming

To convert boolean to integer, let us first declare a variable of boolean primitive.

boolean bool = true;

Now, to convert it to integer, let us now take an integer variable and return a value “1” for “true” and “0” for “false”.

int val = (bool) ? 1 : 0;

Let us now see the complete example to convert boolean to integer in Java.


 Live Demo

public class Demo {
   public static void main(String[] args) {
      // boolean
      boolean bool = true;
      System.out.println("Boolean Value: "+bool);
      int val = (bool) ? 1 : 0;
      // Integer
      System.out.println("Integer value: "+val);


Boolean Value: true
Integer value: 1
Updated on 26-Jun-2020 10:48:50