Now, in life, a statement can either be true or false. It cannot be in-between. How wrong a statement is, is another problem. In fact, a long statement said to not be entirely true is made up of shorter statements, each of which is either true in itself or wrong in itself. Also, a statement can be said to be not false or not true. These three settings of statements give rise to what is known as the three basic truth tables, which are the AND truth table, the OR truth table and the NOT (for not-false or not-true) truth table. AND, OR, and NOT are known as logic operators.
AND Truth Table
The AND truth table is:
false AND true = false
true AND false = false
true AND true = true
If a false statement is ANDed (operated) with another false statement, the result is false. Continuing, if a false statement is ANDed with a true statement, the result is still false. If a true statement is ANDed with a false statement, the result is also false. Finally, if a true statement is ANDed with another true statement, the result is true.
OR Truth Table
The OR truth table is:
false OR true = true
true OR false = true
true OR true = true
If a false statement is ORed (operated) with another false statement, the result is false. Continuing, if a false statement is ORed with a true statement, the result is true. If a true statement is ORed with a false statement, the result is also true. Finally, the result is true if a true statement is ORed with another true statement.
NOT Truth Table
The NOT truth table is:
! true = false
where ! means, NOT. That is, if a statement is not true, it is false. Also, if a statement is not false, it is true.
Statement/Expression
The word “statement” as expressed above, refers to a statement in the English language. In Java, such equivalent statements are Java expressions. A statement in Java is slightly different. In Java, a statement is an expression that ends with a semicolon.
In Java, an expression can result in true or false. Remember, true or false is a boolean value.
The Java Logical & Operator
This implements the AND truth table, as shown in the following program:
The output is:
false
false
true
as expected.
Conditional-And Operator, &&
The operator, && can be an alternative to the principal Java logical & operator, as illustrated in the following program:
The output is:
false
false
true
same as above.
Conditional-Or Operator, ||
The || operator implements the OR truth table, as demonstrated in the following program:
The output is:
true
true
true
as expected.
Logical Complement Operator, !
This implements the NOT truth table as shown in the following program:
The output is:
false
The Java Exclusive OR operator, ^
The ^ operator is like the strict OR operator. Its truth table is:
false ^ true = true
true ^ false = true
true ^ true = false
Note that this truth table differs from the basic OR truth table, only in the last row, where true-OR-true results in false and not true. The following program, shows the ^ operator in action:
The output is:
true
true
false
The Java Inclusive OR operator, |
The | operator has the same truth table as the basic OR truth table; however, its precedence is the lowest of all the logical operators. The truth table is:
false | true = true
true | false = true
true | true = true
The following program, shows the | operator in action:
The output is:
true
true
true
Strictly speaking, in Java, Boolean Logical Operators are &, ^ and |, with | having the lowest precedence among the three.
Conclusion
Basic logic operators in Java are &, which is almost the same as &&; | which is almost the same as || ; and ! . Logical operators, are operators that are used directly in Boolean logic. ^ is also a logical operator. They are used in if-conditions and while-conditions. Java has other operators that return a true or a false, but they are not called logical operators.