Java boolean Data Type with Example

Prerequisite: Data Types in Java

In this article you will see the use of boolean data type in Java along with suitable examples.

Java boolean Data Type with Example

boolean:

boolean keyword which is a primitive data type. It stores two possible values i.e true or false. This is considered as the basis for many conditions or comparisons. The default value of boolean variable is false.

All relational operators returns this boolean value. It is also used in case of conditional statements or loops.

boolean can be used with variables or methods.

Synatx to declare boolean variable:

boolean varName = true/false;

Where,

  • varName refers to name of the variable.
  • true/false refers to the value of variable i.e. either true or false.

Syntax to declare boolean method:

boolean methodName()
{
   //method body
}

Where,

  • boolean refers to the type of method.
  • methodName refers to name of the method.

Let’s see some example programs to understand it more clearly.

Example-1: Declare boolean variables with true/false value and Print it

Approach:

  • Here we have taken two boolean variables say varOne and varTwo¬†and initialized it with true and false value respectively.
  • Then printing the values of those variables.

Program:

class Main 
{
    public static void main(String[] args)
    {
        boolean varOne = true ;
        boolean VarTwo = false;
        System.out.println(varOne);
        System.out.println(VarTwo);
    }
}
Output:

true
false

Example-2: boolean value result with conditional operators

Approach:

  • Here we have declared an boolean expression by using conditional operators.
  • And printing the output as true or false.

Program:

import java.util.*;

class Main 
{
    public static void main(String[] args)
    {
        //declared two integer variables i.e. number1 and number2
        //inituialized values of both variables
        int number1 = 50;
        int number2 =21;
        //comparing two values using comparision operator
        // as result it returns true or false value
        System.out.println(number1 < number2);  // checking is number1 is less than number2
        System.out.println(number1 > number2);  // checking is number1 is greater than number2
        System.out.println(number1 == number2); // checking are both number1 and number2 equal
    }
}
Output:

false
true
false

Example-3: Declare boolean type method

Approach:

  • Here we have taken a user defined method of type boolean means return type is boolean so it will return true or false value as result.

Program:

import java.util.*;

class Main 
{
    public static void main(String[] args)
    {
        //object of Scanner class is created
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a number: ");
        int number = sc.nextInt();
        //calling a user defined method say checkNumber() and passing 'number' as parameter
        //declared boolean type variable say result to store the result of checkNumber() method
        boolean result=checkNumber(number);
        System.out.print("is entered number greater than 50? "+result);
    }
    
    //checkNumber() method to check numbner is greater than 50 or not
    //return type of method is boolean
    public static boolean checkNumber(int number)
    {
        //if number is greater than 50 then return true
        if(number>50)
            return true;
        //else return false
        else
            return false;
    }
}
Output:

Case-1
Enter a number: 68
is entered number greater than 50? true

Case-2
Enter a number: 7
is entered number greater than 50? false