Convert Boolean to Integer in Java



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

Open Compiler
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
Updated on: 2024-06-25T14:39:45+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements