Java: Logical Operators

bitwise operators in java


Java provides quite a variety of operators which come in handy to the programmer for the manipulation of variables.

The operators in java are as follows:

  • Arithmetic Operators
  • Relational Operators
  • Bitwise Operators
  • Logical Operators
  • Assignment Operators

Logical Operators

Logical operators are mainly used to control program flow. They allow a program to make a decision based on multiple conditions.

&&(Logical AND)It returns true if both statements are truea && b
||(Logical OR)It returns true if one of the statements is truea || b
!(Logical NOT)It reverses the result, returns false if the result is true!(a == b)

Syntax with Example

public class Main {

   public static void main(String args[]) {
      boolean a = true;
      boolean b = false;

      System.out.println("a && b = " + (a&&b));
      System.out.println("a || b = " + (a||b) );
      System.out.println("!(a && b) = " + !(a && b));


a && b= false

a || b= true

!(a && b)= true