Java

Logical Operators in Java

This article explains some basics of logic and its corresponding operators in Java. This deals with Boolean variables and values. A Boolean value is either true or false; that is all. So, there are only two boolean values; simple! In Java, an integer variable can hold the number 2, or 3 or 4, etc. There is also a type called boolean in Java, which can hold either true or false values.

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 false = false

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 false = false

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:

! false = true

! 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:

    public class TheClass {
        public static void main(String[] args) {
boolean var1 = false &false;
boolean var2 = false &true;
boolean var3 = true &false;
boolean var4 = true &true;

System.out.println(var1 +"\n"+ var2 +"\n"+ var3 +"\n"+ var4);
        }
    }

The output is:

false

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:

    public class TheClass {
        public static void main(String[] args) {
boolean var1 = false &&false;
boolean var2 = false &&true;
boolean var3 = true &&false;
boolean var4 = true &&true;

System.out.println(var1 +"\n"+ var2 +"\n"+ var3 +"\n"+ var4);
        }
    }

The output is:

false

false

false

true

same as above.

Conditional-Or Operator, ||

The || operator implements the OR truth table, as demonstrated in the following program:

    public class TheClass {
        public static void main(String[] args) {
boolean var1 = false || false;
boolean var2 = false || true;
boolean var3 = true || false;
boolean var4 = true || true;

System.out.println(var1 +"\n"+ var2 +"\n"+ var3 +"\n"+ var4);
        }
    }

The output is:

false

true

true

true

as expected.

Logical Complement Operator, !

This implements the NOT truth table as shown in the following program:

    public class TheClass {
        public static void main(String[] args) {
boolean var1 = ! false;
boolean var2 = ! true;

System.out.println(var1 +"\n"+ var2);
        }
    }

The output is:

true

false

The Java Exclusive OR operator, ^

The ^ operator is like the strict OR operator. Its truth table is:

false ^ false = false

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:

    public class TheClass {
        public static void main(String[] args) {
boolean var1 = false ^ false;
boolean var2 = false ^ true;
boolean var3 = true ^ false;
boolean var4 = true ^ true;

System.out.println(var1 +"\n"+ var2 +"\n"+ var3 +"\n"+ var4);
        }
    }

The output is:

false

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 | false = false

false | true = true

true | false = true

true | true = true

The following program, shows the | operator in action:

    public class TheClass {
        public static void main(String[] args) {
boolean var1 = false | false;
boolean var2 = false | true;
boolean var3 = true | false;
boolean var4 = true | true;

System.out.println(var1 +"\n"+ var2 +"\n"+ var3 +"\n"+ var4);
        }
    }

The output is:

false

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.

About the author

Chrysanthus Forcha

Discoverer of mathematics Integration from First Principles and related series. Master’s Degree in Technical Education, specializing in Electronics and Computer Software. BSc Electronics. I also have knowledge and experience at the Master’s level in Computing and Telecommunications. Out of 20,000 writers, I was the 37th best writer at devarticles.com. I have been working in these fields for more than 10 years.