Java Program to convert integer to boolean
Last Updated :
02 Jan, 2020
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:
public class GFG {
public static void main(String[] args)
{
int intValue = 1 ;
boolean boolValue;
if (intValue >= 1 ) {
boolValue = true ;
}
else {
boolValue = false ;
}
System.out.println(
intValue
+ " after converting into boolean = "
+ boolValue);
}
}
|
Output:
1 after converting into boolean = true
Example 2:
public class GFG {
public static void main(String[] args)
{
int intValue = 0 ;
boolean boolValue;
if (intValue >= 1 ) {
boolValue = true ;
}
else {
boolValue = false ;
}
System.out.println(
intValue
+ " after converting into boolean = "
+ boolValue);
}
}
|
Output:
0 after converting into boolean = false
Share your thoughts in the comments
Please Login to comment...