Tutorials

Java Operators


Logical Operators


In java we have three logical operators:

  1. && (AND),
  2. || (OR), and
  3. ! (NOT).

They are used with one or more relational expressions and result in either true or false.

 

Logical Operator in java

Logical Operators

Meaning

Example

Output

$$ (AND)

True if both conditions are true, and false otherwise

(5==5) && (3>6)

(10>=) && (3 !=5)

False

True

|| (OR)

True if either of its two conditions are true, false if both the conditions are false

(5==5) || (3>6)

(5>=10) || (3==5)

True

False

! (NOT)

False if the condition is true and true if the condition is false

!(5==5)

!(6<=4)

!((5==5) && (3>6))

!((5==) ||(3>6))

False

True

True

False

Precedence of Logical operator is !, && and then ||.

It means in an expression:

  • first ! is solved.
  • then && is solved and
  • then || is solved.


Please Share this page
Views : 45