java cast boolean to int

java cast boolean to int

In Java, you can cast a boolean value to an int using the following syntax:

‮efer‬r to:lautturi.com
int value = (int) booleanValue;

This will set the value of value to 1 if booleanValue is true, and 0 if booleanValue is false.

Here's an example:

boolean flag = true;
int value = (int) flag;

System.out.println(value); // Outputs: 1

Note that this is not the recommended way to convert a boolean value to an integer. A more common approach is to use an if statement or a ternary operator to explicitly set the value of the integer based on the boolean value. For example:

int value = flag ? 1 : 0;

This will set the value of value to 1 if flag is true, and 0 if flag is false.

Created Time:2017-11-03 00:14:48  Author:lautturi