Java Program to convert an Integer to a boolean specifying the conversion values

Java 8Object Oriented ProgrammingProgramming

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
raja
Published on 13-Nov-2018 07:34:12
Advertisements