Java Logical Operators and Examples

In Java, Logical Operators compares two boolean values and returns a boolean result. In this tutorial, the usage and examples of java logical operators are introduced.

Java Logical Operators and Examples

&& Operator (Logical AND Operator)

&& (Logical AND) operator returns true if both of the conditions are true.

 

|| Operator (Logical OR Operator)

|| (Logical OR) operator returns true if one of the conditions are true.

 

! Operator (Logical NOT Operator)

! (Logical NOT) operator works with one condition. Returns true if the condition is false, returns false if the condition is true.

 

Examples of Logical Operators

boolean a = (5>3 && 4>2);
System.out.println(a); // true

boolean b = (5>3 || 4>2);
System.out.println(b); // true

boolean c = !(5>3 && 4>2);
System.out.println(c); // false

boolean d = (false && true);
System.out.println(d); // false

boolean e = (false || true);
System.out.println(e); // true

boolean f = !(false && false);
System.out.println(f); // true

boolean g = !(!(true || 4>5));
System.out.println(g); // true

 

Complex Examples and Nested Examples for Logical Operators

boolean a = !(2*3>5 && false) || 2%3==2;
System.out.println(a); // true

boolean b = (!a && (5>=5));
System.out.println(b); // false