Table of Contents

    Boolean Data Type in Java: Definition and Usage Explained

    Boolean Data Type in Java: Definition and Usage Explained

    boolean Data Type in Java

    Boolean category data type is used for representing or storing logical values is true or false. In java programming to represent Boolean values or logical values, we use a data type called Boolean.Values of type boolean are not converted implicitly or explicitly (with casts) to any other type. But the programmer can easily write conversion code.

    • boolean is primitive data type in Java.
    • boolean data type represents one bit of information
    • Boolean data type is used for logical values.
    • There are only two possible values: true and false
    • This data type is used for simple flags that track true/false conditions
    • Default value is false
    • "Boolean" is wrapper class for "boolean" primitive data type.
    • Example:
      boolean one = true;

    Variable Declaration and Variable Initialization:

    Variable Declaration : To declare a variable , you must specify the data type & give the variable a unique name.

    boolean flag;

    Variable Initialization : To initialize a variable you must assign it a valid value.

    flag = true;

    Variable Declaration and Variable Initialization in two Step :

    boolean flag;
    flag = true;
    Figure:


    Example :

    Save Source File Name as : HelloWorld.java
    To compile : javac HelloWorld.java
    To Run : java HelloWorld
    // A Java program to demonstrate boolean data type
    class HelloWorld { 
       public static void main(String args[])
         { 
               boolean flag; 
               flag = true;
               System.out.println(flag); 
         } 
       }
    Output
    true
    variable declaration in java

    Variable Declaration and Variable Initialization in one Step :

    boolean flag = true;
    Figure:



    Example :

    // A Java program to demonstrate boolean data type
    class HelloWorld { 
       public static void main(String args[])
         { 
               boolean flag = true;
               System.out.println(flag); 
         } 
       }
    Output
    true

    A Java program to demonstrate boolean data type

    Example 1:

    Save Source File Name as : BooleanExample1.java
    To compile : javac BooleanExample1.java
    To Run : java BooleanExample1
    // A Java program to demonstrate boolean data type
    class BooleanExample1
    {
        public static void main(String args[]) 
        {
            boolean b = true;       
            if (b == true)
            System.out.println("Hi Geek");
        }    
    }
    Output:
    Hi Geek

    Example 2:

    Save Source File Name as : BooleanExample2.java
    To compile : javac BooleanExample2.java
    To Run : java BooleanExample2
    // A Java program to demonstrate boolean data type
    class BooleanExample2
    {
    public static void main(String args[]) {
    
     boolean a,b,c;
    
     a  = true;  // Assigning Value
     b  = false; // Assigning Value
     c  = b;    // Assigning Variable
    
     System.out.println(a); // Printing Value
     System.out.println(b); // Printing Value
     System.out.println(c); // Printing Value
    
     }
    }
    Output:
    true
    false
    false

    Different Ways of Using Boolean Value :

    Way 1 : Inside If Statement

    class BooleanExample3
    {
    public static void main(String args[])
    {
     boolean b;
    
     b = true;
    
     if(b)
       {
       System.out.println("I am True");
       }
     }
    }
  • Boolean Value is used to check whether condition is true or not.
  • No need to do == to check equality
    class BooleanExample4
    {
    public static void main(String args[])
    {
     boolean b;
    
     b = true;
    
     if(b == true)
       {
       System.out.println("I am True");
       }
     }
    }

    Way 2 : Comparing Two Numbers

    class BooleanExample5 {
    
    public static void main(String args[]) {
    
     boolean b;
    
     b = (10 > 6);
    
     if(b)
       {
       System.out.println("10 > 6");
       }
     }
    }

    we can use boolean value to hold the result Comparison operators. here 10 > 6 therefor true will be stored in boolean variable