Logical Operators in Java

Notes:

Logical Operators in Java Programming Language:

Logical operators are also known as logical connectives. They are used to connect one or more relational expressions (or conditional expressions). They accept Boolean values as operands (i.e. true or false), on evaluation they yield the result either true or false

- There are 3 logical operators available in Java. They are

&&: Logical And Operator:
If both LHS and RHS operands are true then the result will be true, in all other cases the result will be false

||: Logical Or Operator:
If both LHS and RHS operands are false then the result will be false, in all other cases the result will be true

!: Logical Not Operator:
If the given operand is true, then the result will be false. Vice versa

Example Code:
package logicaloperatorsdemo;

public class LogicalOperatorsDemo
{
public static void main(String[] args)
{
System.out.println((3<4) && (4<5)); // true
System.out.println((3<4) && (4>5)); // false
System.out.println((3>4) && (4<5)); // false
System.out.println((3>4) && (4>5)); // false
System.out.println();
System.out.println((3<4) || (4<5)); // true
System.out.println((3<4) || (4>5)); // true
System.out.println((3>4) || (4<5)); // true
System.out.println((3>4) || (4>5)); // false
System.out.println();
System.out.println(!(3<4)); // false
System.out.println(!(4>5)); // true
System.out.println();
System.out.println(true && true); // true
System.out.println(true && false); // false
System.out.println(false && true); // false
System.out.println(false && false); // false
System.out.println();
System.out.println(true || true); // true
System.out.println(true || false); // true
System.out.println(false || true); // true
System.out.println(false || false); // false
System.out.println();
System.out.println(!true); // false
System.out.println(!false); // true
}
}