Java Program to convert boolean to integer

Given a boolean value, the task is to convert this boolean value into an integer value in Java.

Examples:

Input: boolean = true
Output: 1

Input: boolean = false
Output: 0

Approach:

  • Get the boolean value to be converted.
  • Check if boolean value is true or false
  • If the boolean value is true, set the integer value as 1.
  • Else if the boolean value is false, set the integer value as 0.

Below is the implementation of the above approach:

Example 1: When boolean value is true



filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to convert boolean to integer
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // The boolean value
        boolean boolValue = true;
  
        // The expected integer value
        int intValue;
  
        // Check if it's true or false
        if (boolValue) {
            intValue = 1;
        }
        else {
            intValue = 0;
        }
  
        // Print the expected integer value
        System.out.println(
            boolValue
            + " after converting into integer = "
            + intValue);
    }
}

chevron_right


Output:

true after converting into integer = 1

Example 2: When boolean value is false

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to convert boolean to integer
  
public class GFG {
  
    public static void main(String[] args)
    {
  
        // The boolean value
        boolean boolValue = false;
  
        // The expected integer value
        int intValue;
  
        // Check if it's true or false
        if (boolValue) {
            intValue = 1;
        }
        else {
            intValue = 0;
        }
  
        // Print the expected integer value
        System.out.println(
            boolValue
            + " after converting into integer = "
            + intValue);
    }
}

chevron_right


Output:

false after converting into integer = 0

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.