Java Boolean Operator Explained [Practical Examples]


JAVA

Reviewer: Deepak Prasad

Introduction to Java Boolean Operators

The java Boolean operator is also a datatype just like int, float, or char. It is used where the condition true or false is needed, where the answer needs to be either 1 or 0. 1 being true and 0 being false. By default, a Boolean variable is false, which can change afterward.

 

Declaration

The java Boolean operator can be declared as

 boolean variable_name;

shown in the piece of code below:

public class operator {
 public static void main(String[] args) {

   // declare a boolean operator
   boolean bool_operator = false;

           // the given statement will return the value assigned to the operator.
           // shows value of the operator
          System.out.println("Hello, the value of this bool operator is :"+bool_operator);
       }
}

 

Output :

Hello, the value of this bool operator is :false

In the above code, I have declared a boolean operator named as bool_operator and then assigned it a value, which is false.

 

Types of  Java Boolean operator 

In Java, there are many different types of operators that return boolean values, different types of java boolean operator  is listed below:

Name Operator
Logical AND &&
Logical OR ||
Equals to !ERROR! unexpected operator '='
XOR ^
NOT ^
Greater than >
Less than <
Greater than or Equals to >=
Less than or Equals to <=
Not Equals to !=

 

Output of different logical operators in Java:

A B A&&B A||B A^B !A A!=B A==B
True True True True False False False True
True False False True True False True False
False True False True True True True False
False False False False False True False True

 

1. Logical AND Operator

The & operator and && operator both are used to check if both the expressions that are being compared are true or not, if one of them is false or both of them are false, it returns false, otherwise true. The example shown in the code attached below:

package boolean_operator;                

public class operator {

       public static void main(String[] args) {

           // this code snippet checks the java boolean operator &&
           // declaring boolean variables.
           boolean bool_operator1= true;
           boolean bool_operator2=true;
           boolean bool_operator3=false;
           boolean bool_operator4=false;

          // the statements executed below will show different values of operands using AND conditions
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator1&&bool_operator2));//A&&B
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator1&&bool_operator3));//A&&B
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator3&&bool_operator4));//A&&B
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator2&&bool_operator4));A&&B
       }
}

The output of the following code is

Hello, the value of this bool expression is :true
Hello, the value of this bool expression is :false
Hello, the value of this bool expression is :false
Hello, the value of this bool expression is :false

The only time it returns true is when both operator 1 and operator 2 are true, in all the other cases the value returned will be false.

 

2. Logical OR Operator

The logical OR operator is | or || it returns true only either one of the two operands are true, it returns false if both the operands are false, the given code snippet will show how logical OR operator works.

package boolean_operator;

public class operator {
       public static void main(String[] args) {

           // this code snippet checks the java boolean operator || 
           //declare boolean variables
           boolean bool_operator1= true;
           boolean bool_operator2=true;
           boolean bool_operator3=false;
           boolean bool_operator4=false;

          // the statements below will execute different operand values using or condition
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator1||bool_operator2)); 
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator1||bool_operator3));
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator3||bool_operator4));
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator2||bool_operator4));
       }
}

The output of the following code is :

Hello, the value of this bool expression is :true
Hello, the value of this bool expression is :true
Hello, the value of this bool expression is :false
Hello, the value of this bool expression is :true

 

3. The Equal to (==) Operator

The equal to operator i.e. == returns true only if operand one and operand two are equal, in any other case it returns false. The example is shown in the code below :

package boolean_operator;

public class operator {
     public static void main(String[] args) {

        // this code snippet checks the java boolean operator ==
        // declare two variables for comparison
        int bool_operator1= 1;
        int bool_operator2=2;

        // the output will show if both the operands are equal or not
        System.out.println("Hello, the value of this bool expression is :"+(bool_operator1==bool_operator2));
     }
}

The output of this code is :

Since operator 1 and operator 2 are not equal, value returned is false.

Hello, the value of this bool expression is :false

 

4. The XOR (^) Operator

The XOR operator returns true only if the two operands are different, it returns false if the operands are same.

package boolean_operator;
public class operator {
       // this code snippet checks the java boolean operator ^
       public static void main(String[] args) {

           // declaring two variables
           boolean bool_operator1= true;
           boolean  bool_operator2=true;

          // XOR operator will return true if both of them are different.
          System.out.println("Hello, the value of this bool expression is :"+(bool_operator1^bool_operator2));
       }
}

 

The output of the following code is false since both the operands are the same.

Hello, the value of this bool expression is :false

 

5. The logical NOT (!) Operator

The NOT operator returns true if the operand is false and false if the operand is true. The given code snippet will explain how the NOT operator works.

package boolean_operator;

public class operator {

       public static void main(String[] args) {

           boolean bool_operator1= true;
           boolean  bool_operator2=false;

          // this code snippet checks the java boolean operator !
          // the output will be true if the operand is false, and false if the operand is true
          System.out.println("Hello, the value of this bool expression is :"+(!bool_operator1));
          System.out.println("Hello, the value of this bool expression is :"+(!bool_operator2));
       }
}

The output of the following code is :

Hello, the value of this bool expression is :false
Hello, the value of this bool expression is :true

As shown in output, it returns the reverse value of the operand.

 

6. Greater than (>) Operator

The greater than operator returns true if the left operand is greater than the right operand, otherwise false.

Example shown in code below

 

7. Less than (<) Operator

The less than operator returns true if the left operand is less than the right operand, otherwise false.

Example shown in code below

 

8. Greater than or equal to (>=) Operator

The greater than or equal operator returns true if the left operand is greater than or equals to the right operand, otherwise false.

The example shown in code below

 

9. Less than or equal to (<=) Operator

The greater than or equal operator returns true if the left operand is less than or equals to the right operand, otherwise false.

The example shown in code below

 

10. Not equal to (!=) Operator

The greater than or equal operator returns true if the left operand is not equal to the right operand, otherwise false. The example is shown in the code below.

package boolean_operator;
public class operator {

       // this code snippet checks the java boolean operators >,<,<=,>=,!=
       public static void main(String[] args) {

          // declare operand 1
           int operand1= 1;

          // declare operand 2 
           int operand2=2;

          // compare
          // greater than
          System.out.println("Hello, the value of this bool expression is :"+(operand1>operand2));

          // less than
          System.out.println("Hello, the value of this bool expression is :"+(operand1<operand2));

          // greater than equals to
           System.out.println("Hello, the value of this bool expression is :"+(operand1>=operand2));

          // less than equals to
          System.out.println("Hello, the value of this bool expression is :"+(operand1<=operand2));
 
          // not equals
          System.out.println("Hello, the value of this bool expression is :"+(operand1!=operand2));
       }
}

The output of the following code will be as follows

Hello, the value of this bool expression is :false
Hello, the value of this bool expression is :true
Hello, the value of this bool expression is :false
Hello, the value of this bool expression is :true
Hello, the value of this bool expression is :true

 

Practice Code

Here is a code snippet that will help you practice the java boolean operator, determine the output of the code by dry runs and then run it on your compiler

package boolean_operator;

// code to practice java boolean operator.
public class operator {

         public static void main(String[] args) {

              // declaring variables to check conditions.
              int operand1= 1; 
              int operand2=2;

              // given below are different conditions to check java boolean operator
              System.out.println("Hello, the value of this bool expression is :"+(operand1==operand2));
              System.out.println("Hello, the value of this bool expression is :"+((operand1>operand2)&&(operand2<operand1)));
              System.out.println("Hello, the value of this bool expression is :"+((operand1>operand2)||(operand2<operand1)));
              System.out.println("Hello, the value of this bool expression is :"+(operand1!=operand2));
              System.out.println("Hello, the value of this bool expression is :"+(operand1>operand2));
              System.out.println("Hello, the value of this bool expression is :"+(operand1<operand2));
              System.out.println("Hello, the value of this bool expression is :"+(operand1>=operand2));
              System.out.println("Hello, the value of this bool expression is :"+(operand1<=operand2));
              System.out.println("Hello, the value of this bool expression is :"+(operand1!=operand2));
         }
}

Determine the output.

 

Conclusion

Java boolean operator is widely used in programming, In most programming practices many conditional statements form the basis of logics implemented, the java conditional operators tends to form faster and efficient logics,  java supports different types of logical operators that include logical operators, like the logical AND and logical OR as discussed above, unary operators like NOT and bitwise operators like >,< all of these operators are discussed in the above article.

 

What's Next

Java Byte Explained [Easy Examples]

 

Further Reading

java operators
summary of java operators

 

Azka Iftikhar

Azka Iftikhar

She is proficient in multiple programming languages, including C++, GO, and Java, she brings a versatile skillset to tackle a wide array of challenges. Experienced Computer Scientist and Web Developer, she excels as a MERN Stack Expert. You can check her professional profile on GitHub which captures her experience and portfolio.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment