What are the Boolean Values in Java?

A Boolean expression is a Java expression that returns a Boolean value: true or false . Very often, in programming, you will need a data type that can only have one of two values, like:

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, Java has a boolean data type, which can take the values true or false.

Boolean Values

A boolean type is declared with the boolean keyword and can only take the values true or false:

Example

boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println(isJavaFun);     // Outputs true
System.out.println(isFishTasty);   // Outputs false

However, it is more common to return boolean values from boolean expressions, for conditional testing (see below).

Boolean Expression

Boolean expression is a Java expression that returns a Boolean value: true or false. You can use a comparison operator, such as the greater than (>) operator to find out if an expression (or a variable) is true:

Example

int x = 10;
int y = 9;
System.out.println(x > y); // returns true, because 10 is higher than 9

Or even easier:

Example

System.out.println(10 > 9); // returns true, because 10 is higher than 9

In the examples below, we use the equal to (==) operator to evaluate an expression:

Example

int x = 10;
System.out.println(x == 10); // returns true, because the value of x is equal to 10

Example

System.out.println(10 == 15); // returns false, because 10 is not equal to 15

How do you add a Boolean in Java?

To display Boolean type, firstly take two variables and declare them as boolean. val1 = true; Now, use if statement to check and display the Boolean true value. if(val1) System.

Examples of Java boolean keyword

Example 1: Simple boolean example

public class BooleanExample1 {  
  
    public static void main(String[] args) {  
      
        int num1=10;  
        int num2=20;  
          
    boolean b1=true;  
    boolean b2=false;  
  
if(num1<num2)  
{  
    System.out.println(b1);  
}  
else  
{  
    System.out.println(b2);  
}  
          
    }         
}  

Output:

true

Example 2: Comparing the variables of boolean type

public class BooleanExample2 {  
  
    public static void main(String[] args) {  
      
    boolean b1=true;  
    boolean b2=false;  
    boolean b3=(b1==b2);  
      
    System.out.println(b1);  
    System.out.println(b2);  
    System.out.println(b3);  
          
    }         
}  

Output:

true
false
false

Example 3: Method of boolean type

public class BooleanExample3 {  
  
    public static boolean display(int num)  
    {  
        if(num>10)  
        {  
            return true;  
        }  
        else  
        {  
        return false;  
    }  
        }  
      
      
    public static void main(String[] args) {  
      
          
        System.out.println(display(15));  
          
    }         
}  

Output:

true

Example 4: Comparing objects

public class BooleanExample4 {  
  
    public static void main(String[] args) {  
  
        BooleanExample4 b1=new BooleanExample4();  
        BooleanExample4 b2=new BooleanExample4();  
          
        boolean result=(b1==b2);  
        System.out.println("Is objects are equal : "+result);  
                  
    }         

Output:

Is objects are equal : false

Example 5: Finding a prime number

public class BooleanExample5 {  
  
    
    
    public static void main(String[] args) {  
  
        int num=7;  
        boolean flag=false;  
  
          for(int i=2;i<num;i++)  
          {  
              if(num%i==0)  
              {  
                  flag=true;  
                          break;  
              }  
          }  
        if(flag)  
        {  
            System.out.println("Not prime");  
        }  
        else  
        {  
            System.out.println("prime");  
        }  
          
    }         
}  

Output:

prime
What are the Boolean Values in Java?
Show Buttons
Hide Buttons