Logical Operators in Java
A logical operator (sometimes called a "Boolean operator") in Java programming is an operator that returns a Boolean result that's based on the Boolean result of one or two other expressions. Sometimes, expressions that use logical operators are called "compound expressions" because the effect of the logical operators is to let you combine two or more condition tests into a single expression. Logical operators when we test more than one condition to make decisions. These are: && (meaning logical AND), || (meaning logical OR) and ! (meaning logical NOT).
| Operator | Example | Meaning |
|---|---|---|
| && | (Logical AND) | expression1 && expression2 true only if both expression1 and expression2 are true |
| || | (Logical OR) | expression1 || expression2 true if either expression1 or expression2 is true |
| ! | (Logical NOT) | !expression true if expression is false and vice versa |
Code Snippet
//Logical Operators in Java
public class App {
public static void main(String[] args) {
int m1 = 25, m2 = 75;
System.out.println("And && : " + (m1 >= 35 && m2 >= 35));
System.out.println("Or || : " + (m1 >= 35 || m2 >= 35));
}
}