Java Program to convert integer to boolean

To convert integer to boolean, initialize the bool value with integer. Now, use the == operator to compare the int with a value and if there is a match, TRUE is returned.

Let’s first create an integer value and initialize:

int a = 5;

Declare a boolean, but do not assign any value:

boolean b;

Now, using the == operator for comparison and convert the int value to boolean:

Example 1

public class Example {

  public static void main(String[] args) {
   
    int a = 5;
    
    boolean b = (a == 10);
    System.out.println("It is a match = "+b);
  }
}

Output

It is a match = false

Let us see another example with a different approach:

Example 2

public class Example {

  public static void main(String[] args) {
   
    int a = 5;
    
    boolean b;
    
    if(a > 5) {
         b = true;
         System.out.println(b);
    }
    
    else {
        b = false;
        System.out.println(b);
    }

  }
}

Output

false
Compare two Boolean Arrays in Java
Java Program to convert int to boolean without specifying boolean type
Studyopedia Editorial Staff
[email protected]

We work to create programming tutorials for all.

No Comments

Post A Comment