Sunday 2 February 2014

Logical Operators

Java provides three logical operators, These are &&(AND), || (OR), and !(NOT)

Logical AND is also called Conditional AND

Logical OR is also called Conditional OR

Logical AND, OR are called Short circuit operators, will see why these are called short circuit soon.

Logical AND Operator
Operand1 Operand2 Evaluates To
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE

As shown in the above table, && operator returns true if both the operands evaluates to true, otherwise returns false.

Example
class AndEx{
 public static void main(String args[]){
  boolean operand1 = true;
  boolean operand2 = true;
  System.out.println((operand1 && operand2)); 

  operand1 = true;
  operand2 = false;
  System.out.println((operand1 && operand2)); 

  operand1 = false;
  operand2 = true;
  System.out.println((operand1 && operand2)); 

  operand1 = false;
  operand2 = false;
  System.out.println((operand1 && operand2));
 }
}

Output
true
false
false
false

Why Logical AND is called short circuit operator
Since if the first statement in the expression evaluates to false, then java won't evaluates the entire expression. So Logical AND is called short circuit AND

Example
class ShortCircuitAnd{
 public static void main(String args[]){
  int a = 10, b=21;
  
  if( (a>b) && (a++ > b) ){
   System.out.println("This statement not evaluated");
  }
  System.out.println("a is not incremented " + a);
  
  if( (a<b) && (a++ < b) ){
   System.out.println("This statement is evaluated");
  } 
  System.out.println("a is not incremented " + a);
  
 }
}
   
Output
a is not incremented 10
This statement is evaluated
a is not incremented 11
Observation
In the expression (a>b) && (a++ > b), a>b is false, so && operator won't evaluates second statement in the expression, so a is not incremented.

Logical OR Operator
Operand1 Operand2 Evaluates To
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

As shown in the above table, || operator returns true if any of the operand evaluates to true, otherwise returns false.

Example
class OrEx{
 public static void main(String args[]){
  boolean operand1 = true;
  boolean operand2 = true;
  System.out.println((operand1 || operand2)); 

  operand1 = true;
  operand2 = false;
  System.out.println((operand1 || operand2)); 

  operand1 = false;
  operand2 = true;
  System.out.println((operand1 || operand2)); 

  operand1 = false;
  operand2 = false;
  System.out.println((operand1 || operand2));
 }
}
Output
true
true
true
false

Why Logical OR is called short circuit operator
Since if the first statement evaluates to true, then java won't evaluates the entire expression. So Logial OR is called short circuit OR

Example
class ShortCircuitOr{
 public static void main(String args[]){
  int a = 10, b=21;
  
  if( (a<b) || (a++ < b) ){
   System.out.println("This statement evaluated");
  }
  System.out.println("a is not incremented " + a);
  
  if( (a>b) || (a++ > b) ){
   System.out.println("This statement is evaluated");
  } 
  System.out.println("a is incremented " + a);
 }
}
  
Output
This statement evaluated
a is not incremented 10
a is incremented 11

Logical (!)NOT operator
Operand Evalutes To
FALSE TRUE
TRUE FALSE

If the operand is FALSE, ! Operator evaluates it to TRUE
If the operand is TRUE, ! Operator evaluates it to FALSE

Example
class LogicalNot{
 public static void main(String args[]){
  boolean a = true;
  System.out.println((!a));
  
  a = false;
  System.out.println((!a));
 }
}
Output
false
true





Not equal to operator                                                 Bitwise operators                                                 Home

No comments:

Post a Comment