Convert Boolean to Int in Java
-
Convert
boolean
toint
Using a Ternary Operator in Java -
Convert
boolean
toint
Using theif
Condition in Java -
Convert
boolean
toint
Using theApache
Library in Java -
Convert
boolean
toint
Using thecompareTo()
Method in Java -
Convert
boolean
toint
Using theindexOf()
Method in Java -
Convert
boolean
toint
Using theshift
Operator in Java

This tutorial introduces how you can convert boolean
to int
in Java. You can find some example programs as your guide to understand this topic better.
The keyword boolean
is a data type in Java used to hold two values, either true
or false
. On the other hand, int
is used to store the whole number. In this article, you’ll learn how to convert boolean
to int
in Java by using some built-in methods or a custom code. We will use the ternary operator, compareTo() method, and the Apache commons library. Read on!
Convert boolean
to int
Using a Ternary Operator in Java
In this example, we used the ternary operators to convert boolean
values into int
. Based on the boolean
value, we get either 1 or 0 as a result; this is one of the basic and single-line solutions we can apply.
public class SimpleTesting{
public static void main(String[] args) {
boolean b = true;
int i = b ? 1 : 0;
System.out.println(i);
b = false;
i = b ? 1 : 0;
System.out.println(i);
}
}
Output:
1
0
Convert boolean
to int
Using the if
Condition in Java
If you want to use if
conditions, use this code that returns the int
value based on the boolean
counterpart. It returns 1
if the boolean
value is true
, and 0
if the boolean
value is false
. See the example below:
public class SimpleTesting{
public static void main(String[] args) {
boolean b = true;
int result = boolToInt(b);
System.out.println(result);
b = false;
result = boolToInt(b);
System.out.println(result);
}
static int boolToInt(boolean b) {
if(b)
return 1;
return 0;
}
}
Output:
1
0
Convert boolean
to int
Using the Apache
Library in Java
If you’re familiar with the Apache commons library, you can use the toInteger()
method of the BooleanUtils
class. It returns the int
value after the conversion of true or false. Here’s how you can do it:
import org.apache.commons.lang3.BooleanUtils;
public class SimpleTesting{
public static void main(String[] args) {
boolean b = true;
int result = BooleanUtils.toInteger(b);
System.out.println(result);
b = false;
result = BooleanUtils.toInteger(b);
System.out.println(result);
}
}
Output:
1
0
Convert boolean
to int
Using the compareTo()
Method in Java
The compareTo()
method belongs to the Boolean
class, used for comparing two boolean
values and returning an integer value based on the comparison. It returns 0 if both boolean
values are equal, -1 if the value is less, and 1 if the value is greater. Check this sample code:
public class SimpleTesting{
public static void main(String[] args) {
Boolean a = false;
int result = a.compareTo(false);
System.out.println(result);
result = a.compareTo(true);
System.out.println(result);
a = true;
result = a.compareTo(false);
System.out.println(result);
}
}
Output:
0
-1
1
Convert boolean
to int
Using the indexOf()
Method in Java
This process is not a straightforward approach, but you can still use it to convert boolean
values into int
types. This process is a one-line solution you can use to get an integer value. Check this example:
public class SimpleTesting{
public static void main(String[] args) {
boolean b = true;
int i = -("false".indexOf("" + b));
System.out.println(i);
b = false;
i = -("false".indexOf("" + b));
System.out.println(i);
}
}
Output:
1
0
Convert boolean
to int
Using the shift
Operator in Java
You can also use the right shift operator to convert boolean
to int
in Java. The hashCode()
method of the Boolean
class returns the hashcode and is then changed by using the shift operator. See the sample program below:
public class SimpleTesting{
public static void main(String[] args) {
boolean b = true;
int result = 1 & Boolean.hashCode( b ) >> 1;
System.out.println(result);
b = false;
result = 1 & Boolean.hashCode( b ) >> 1;
System.out.println(result);
}
}
Output:
1
0
Related Article - Java Integer
- Handle Integer Overflow and Underflow in Java
- Reverse an Integer in Java
- Convert Int to Binary in Java
- The Max Value of an Integer in Java
- Minimum and Maximum Value of Integer in Java
- Convert Integer to Int in Java
Related Article - Java Boolean
- Toggle a Boolean Variable in Java
- Print Boolean Value Using the printf() Method in Java
- Initialise Boolean Variable in Java
- Boolean to String in Java
- String to Boolean in Java