Convert Boolean to Int in Java

Mohammad Irfan Jan 30, 2023 Jun 07, 2021
  1. Convert boolean to int Using a Ternary Operator in Java
  2. Convert boolean to int Using the if Condition in Java
  3. Convert boolean to int Using the Apache Library in Java
  4. Convert boolean to int Using the compareTo() Method in Java
  5. Convert boolean to int Using the indexOf() Method in Java
  6. Convert boolean to int Using the shift Operator in Java
Convert Boolean to Int 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

Related Article - Java Boolean