Logical Operators
The logical operators give back a value of true or false (boolean).
There are three logical operators in Java and are evaluated in the following order:
! The not operator !true is false !false is the value true
&& The and operator true && true is true, other cases return false
|| The or operator false || false is false, other cases return true
!value result
------ ------
!true false
!false true
value && value result
----- ----- ------
true && true true
true && false false
false && true false
false && false false
value || value result
----- ----- ------
true || true true
true || false true
false || true true
false || false false
Examples:
if (!(x>y))
...
if ( x>y && x>z)
...
if (x<y || x<z)
...
if (x > y)
out.println("x is greater than y");
if (y > x)
out.println("y is greater than x");
if (x > y && x > z)
out.println("x is greater than y and z");
else if (y > z)
out.println("y is greater than x and z");
else
out.println("z is greater than x and y");
Note: && and || operators are short circuited (the right side is not evaluated unless necessary).
For example:
if the left side of an || operation is true, there is no need to evaluate the right hand side.
if the left side of an && operation is false, there is no need to evaluate the right hand side.