- Java Boolean Operator Explained [Practical Examples]
- Introduction to Java Boolean Operators
- Declaration
- Types of Java Boolean operator
- 1. Logical AND Operator
- 2. Logical OR Operator
- 3. The Equal to (==) Operator
- 4. The XOR (^) Operator
- 5. The logical NOT (!) Operator
- 6. Greater than (>) Operator
- 7. Less than ( <) Operator
- 8. Greater than or equal to (>=) Operator
- 9. Less than or equal to ( <=) Operator
- 10. Not equal to (!=) Operator
- Practice Code
- Conclusion
- Java Operator – &, && (AND) || (OR) Logical Operators
- How to use the bitwise AND operator
- How to use the logical AND operator
- How to use the logical OR operator
- Conclusion
Java Boolean Operator Explained [Practical Examples]
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:
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)); // 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)&&(operand2operand2)||(operand2operand2)); 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)); >>
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.
Java Operator – &, && (AND) || (OR) Logical Operators
Ihechikara Vincent Abba
We use operators in most programming languages to perform operations on variables.
They are divided into various categories like arithmetic operators, assignment operators, comparison operators, logical operators, and so on.
In this article, we will be talking about the bitwise AND operator, and the AND ( && ) and OR ( || ) logical operators.
How to use the bitwise AND operator
The symbol & denotes the bitwise AND operator. It evaluates the binary value of given numbers. The binary result of these numbers will be returned to us in base 10.
When the & operator starts its operation, it will evaluate the value of characters in both numbers starting from the left.
Let’s look at an example to help you understand better:
System.out.println(10 & 12); // returns 8
The binary value of 10 is 1010
The binary value of 12 is 1100
Here is something you should have in mind before we start the operation:
So let’s carry out the operation.
The first character for 10 is 1 and the first character for 12 is also 1 so:
We move on to the second characters – 0 for 10 and 1 for 12:
For the third characters – 1 for 10 and 0 for 12:
For the fourth characters – 0 for 10 and 0 for 12:
Now let’s combine all the returned characters. We would have 1000.
The binary value 1000 in base 10 is 8 and that is why our operation returned 8.
How to use the logical AND operator
Note that we use logical operators to evaluate conditions. They return either true or false based on the conditions given.
The symbol && denotes the AND operator. It evaluates two statements/conditions and returns true only when both statements/conditions are true.
Here is what the syntax looks like:
statment1/condition1 && statemnt2/condition2
As you can see above, there are two statements/conditions separated by the operator. The operator evaluates the value of both statements/conditions and gives us a result – true or false.
System.out.println((10 > 2) && (8 > 4)); //true
The operation will return true because both conditions are true – 10 is greater than 2 and 8 is greater than 4. If either one of the conditions had an untrue logic then we would get false .
To better understand the && operator, you should know that both conditions must be true to get a value of true .
Here is another example that returns false :
System.out.println((2 > 10) && (8 > 4)); // false
Here, 2 is not greater than 10 but 8 is greater than 4 – so we get a false returned to us. This is because one of the conditions is not true.
- If both conditions are true => true
- If one of the two conditions is false => false
- If both conditions are false => false
How to use the logical OR operator
We use the symbol || to denote the OR operator. This operator will only return false when both conditions are false. This means that if both conditions are true, we would get true returned, and if one of both conditions is true, we would also get a value of true returned to us.
statment1/condition1 || statemnt2/condition2
Let’s go over a few examples.
System.out.println((6 < 1) || (4 >2)); // true
This returns true because one of conditions is true.
- If both conditions are true => true
- If one of the conditions is true => true
- If both conditions are false => false
Conclusion
In this article, we learned how to use the bitwise & operator in Java and how the operation is carried out to give us a result.
We also learned how to use the && and || logical operators in Java. We learned what value each operation returns based on the conditions involved in the operation.