18 Aug 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:
1 2 3 |
int a = 5; |
Declare a boolean, but do not assign any value:
1 2 3 |
boolean b; |
Now, using the == operator for comparison and convert the int value to boolean:
Example 1
1 2 3 4 5 6 7 8 9 10 11 12 |
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
1 2 3 |
It is a match = false |
Let us see another example with a different approach:
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
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
1 2 3 |
false |
No Comments