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
&& (Logical AND) operator returns true if both of the conditions are true.
|| (Logical OR) operator returns true if one of the conditions are true.
! (Logical NOT) operator works with one condition. Returns true if the condition is false, returns false if the condition is true.
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
boolean a = !(2*3>5 && false) || 2%3==2; System.out.println(a); // true boolean b = (!a && (5>=5)); System.out.println(b); // false