☞Following are the types of logical operators in java.
&& ➺ Logical AND || ➺ Logical OR ! ➺ Logical NOT
☞!(Logical NOT) is an unary operator.
☞&&(Logical AND) and ||(Logical OR) are binary operators.
☞Precedence of logical operators. See the table to understand it properly.
! ➺ unary operator && ➺ binary operators || ➺ binary operators
☞Priority of Logical NOT (!) operator is not only just higher than the remaining two logical operator but higher than all other operators too, as it is also a unary operator.
☞Priority of logical AND (&&) is higher than logical OR (||).
☞It reverses the result. If experssion is True, then it will convert it into False and vice-versa.
exp1 | !exp1 |
---|---|
!false | true |
!true | false |
public class LogicalNotExample { public static void main(String[] args) { int x=5; boolean y=!(x>4); System.out.println(y); } }
false
☞If both the expressions will be true, then result will be true.
exp1 | exp2 | exp1 && exp2 |
---|---|---|
false | false | false |
false | true | false |
true | false | false |
true | true | true |
public class LogicalAndExample { public static void main(String[] args) { int x=5; boolean y=x>4 && x<10; System.out.println(y); } }
true
☞If any expression will be true, then result will be true.
exp1 | exp2 | exp1 || exp2 |
---|---|---|
false | false | false |
false | true | true |
true | false | true |
true | true | true |
public class LogicalOrExample { public static void main(String[] args) { int x=5; boolean y=x>4 || x<10; System.out.println(y); } }
true