Program of Logical operator in java


Java uses logical operators AND(&&),OR(||),NOT(!).These operator yield 1 or 0 depending upon the outcome of different expressions.The different types of logical operators with their format are:

Logical operator Symbol Format
AND && (x>y)&&(x>z)
OR || (x= =y)||(x==z)
NOT ! !(x= =y)

Precedence of logical operators is  NOT(!) then AND(&&) and  then OR(!!).

Logical AND(&&)

The AND operator results in true if both the expressions are true.

e.g  4>2  && 6>3               : result in true because both condition is true

14>22 && 16>7          : result in false because one condition is false.

Logical OR(||)

This operator is used to combine two conditional expressions. It will result in true if either of two conditions is true otherwise false.

e.g  4>2 || 6>8                : result in true because 4 >2 is true.

14>2 || 6>4              : result in true because 6 >4 is true

14>22 || 16>18        : result in false because both condition is false.

Logical NOT(!)

The NOT operator is applied when you want to revert the outcome of an expression.It is a unary operator because it uses a single operand.e.g

!(8>3)       :    false,because 8>3 is true

!(18>31)   :  false,because 18<31 is true

!(8<3)        :  true,because 8<3 is false

Comments are closed.