English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java program converting a boolean value to an integer

To convert a boolean value to an integer, we first declare a boolean primitive variable.

boolean bool = true;

Now, to convert it to an integer, let's use an integer variable and return the value “ 1”, returns the value “ 0” for “ false”.

int val = (bool) ? 1 : 0;

Now let's look at a complete example to convert a boolean value to an integer in Java.

Example

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

Output Result

Boolean Value: true
Integer value: 1