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.

Example

 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);
   }
}

Output

Boolean Value: true
Integer value: 1
raja
Published on 13-Nov-2018 07:29:21
Advertisements