Convert Integer to Boolean in Java with Specified Values



To convert an Integer to a boolean, we have taken the following Integer objects.

Integer one = 1;
Integer two = 1;
Integer three = 0;

We have taken nested if-else statement to display the true or false values. Here, the value “one” is the same as “two” i.e. 1; therefore, the following else-if works.

else if (one.equals(two)) {
   System.out.println(true);
}

The above display “true” and in this way we converted Integer to boolean.

Let us now see the complete example to learn how to convert an Integer to Boolean.

Example

 Live Demo

public class Demo {
   public static void main(String[] args) {
      Integer one = 1;
      Integer two = 1;
      Integer three = 0;
      // Integer to Boolean
      if (one == null) {
         if (two == null) {
            System.out.println(true);
         } else if (three == null) {
            System.out.println(false);
         }
      } else if (one.equals(two)) {
         System.out.println(true);
      } else if (one.equals(three)) {
         System.out.println(false);
      }
   }
}

Output

True
Updated on: 2020-06-26T10:47:23+05:30

213 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements