A Beginner's Guide to Logical and Relational Operators in Java - Android Tricks 4 All
News Update
Loading...

Sunday, July 4, 2021

A Beginner's Guide to Logical and Relational Operators in Java

Operators are symbols used to perform operations on values, variables, or statements. The expressions on which they perform these actions are called operands. The operations return a boolean result (true or false) for relational, equality, and logical operators.

The number of operands an operator takes determines its type. An operator that takes one operand is called "unary". An operator that takes two operands is called "binary".

Read on to learn how you can use logical and relational operators in Java. Better yet, most programming languages use the same operators so you can apply this knowledge elsewhere.

Logical Operators

They are used to build logical statements while programming. There are six logical operators in Java. The table below summarizes them.

Operator Name Type
| Boolean Logical OR Binary
& Boolean Logical AND Binary
^ Boolean Logical Exclusive OR Binary
|| Conditional OR Binary
&& Conditional AND Binary
! Logical NOT Unary

If you want to check whether one or both conditions are true, then use this operator. A condition is an expression that can either be true or false.

Boolean Logical Inclusive OR (|)

The logical OR checks if both operands are true before evaluating the expression.

if ( dob < 2005 | height <= 5){
money++;
}

The above example will give someone more money if their date of birth (dob) is less than 2005 or if their height is less than or equal to 5 feet.

Boolean Logical AND (&)

This operator is used to check if both conditions are true before taking a certain execution path in the program. It first checks if both conditions are true before evaluating the whole expression.

Related: How to Validate Strings Using Boolean Methods in Python

Boolean Logical Exclusive OR (^)

If you want to check whether one of the conditions is true, but not both, then this is the operator to use. The truth table below summarizes the results you'll see when you use it.

expression1 expression2 expression1 ^ expression2
false false false
false true true
true false true
true true false

Boolean Conditional AND (&&)

This operator is similar to the logical AND. The difference is that it first checks if the condition on the left is true before moving on to check the one on the right.

If the left part is found to be false, then execution stops immediately. Otherwise, evaluation of the right part will continue. This feature is known as short-circuit evaluation.

See the truth table below to ground your understanding of this operator.

expression1 expression2 expression1 && expression2
false false false
false true false
true false false
true true true

Conditional OR (||)

If either of the conditions is false, then execution will skip to the next part of the program. In other words, both conditions must be true.

This operator is similar to the Logical OR. It also checks if either one or both of the conditions are true before executing certain code.

Similar to the conditional AND, the logical OR also uses short circuit evaluation. It first checks if the operand on the left is true before evaluating the one on the right.

Related: What Is a Constructor in Java and How Do You Use It?

If the condition on the left is found to be true, then there's no need to check the one the right. Otherwise, evaluation to the right will continue.

Logical NOT (!)

This operator is used to negate a condition. It simply reverses the meaning of what it's operating on.

if(!(x>5)){
// statements
}

The above statement means that if "x is greater than 5" is NOT true, then execute the statements inside the if.

Notice the use of round brackets with the expression (x>5). If you don't include these brackets while writing your program, you will get a compile-time error. The reason is because ! is a unary operator that acts on a condition. Without the brackets, the compiler would interpret it as the operator acting on the x, not x>5.

The inclusion of brackets is not just for enabling the compiler to correctly interpret an expression. They can also be used as a way for the programmer to better understand more complex expressions. Look at the example below:

age >= 7 && height < 5

Some people might find it hard to follow through with the logic. Therefore, some programmers prefer to add redundant parentheses for readability reasons:

(age >= 7) && (height < 5)

Relational Operators

These operators are used to compare simple relations between operands.

Operator Name
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Relational operators are rather easy to understand because they all have the same meaning as the usual algebraic operators you are already familiar with. That is to say, > and < have the same meaning you already know as that given in the table above.

if( x <= 7 ){
x++;
}

The above if statement checks whether x is less than or equal to 7. If true, then the statements inside the brackets execute, otherwise they don't.

Now would be a good time to mention equality operators. There are just two of them ( equal to, == and !=, not equal to ). As their name suggests, they're used to test equality between two operands.

Related: How to Create and Perform Operations on Arrays in Java

The equality operator (==) is not to be confused with the assignment operator (=). Beginner programmers are fond of mixing up the two. This is reasonable since in algebra the symbol (=) is used to express equality. That's not right in programming, though.

The assignment operator (=) assigns a value to a variable while the equality operator (==) tests for equality. See the example below to understand the difference:

if(x=5){
// statements
}

The above code will always execute regardless of whether x is actually equal to 5. Meanwhile, the code below will only execute if x is equal to 5. Therefore, it's important not to mix up the two.

if(x==5){
// statements
}

The two equality operators mentioned have the same level of precedence, though lower than that of relational operators.

Relational operators also have the same level of precedence. Execution of these operators begins from left to right.

Further Considerations of Java Operators

You must have observed that there's whitespace between some operators and their operands in some of the examples while in others, there isn't.

The absence/presence of that space shouldn't worry you. The compiler will ignore it. Therefore, the following expressions mean the same thing:

Y>=7 // no whitespace
Y >= 7 // with whitespace

Relational operators are generally used to express simple conditions. To combine simple conditions into more complex ones, you'll have to use logical operators. Logical operators can test multiple conditions, unlike relational operators which just test one condition.

It's also important to note that the logical operators ( |, &, ^) can be bitwise operators when they have integral operands. When used as bitwise operators, they will be operating on the bits of their operands.

With this knowledge of operators, you should now be gearing up to learn Java classes.



Comments


EmoticonEmoticon

Notification
This is just an example, you can fill it later with your own note.
Done