Java Program to convert integer to boolean

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

Examples:

Input: int = 1
Output: true

Input: int = 0
Output: false

Approach:

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

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:



1 after converting into boolean = true

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

0 after converting into boolean = false

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




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.