Logical Operators

Logical operators are used to construct compound conditions.A Compound is a combination of several simple conditions.Logical operators of 3 types.

  1. && (AND)operator
  2. || (OR) operator
  3. ! (NOT) operator(boolean complement operator)

&& (AND)operator

  1. It performs logical multiplication.
  2. It is a binary operator and it has 2 operands
  3. If atleast one operand is false,result is false
  4. Example:

    if(a==1&&b==1)
    System.out.println("Hi");
    

    If both conditions are true,then only “Hi” will be display

|| (OR) operator

  1. || operator performs logical addition.
  2. It is binary operator.
  3. If at least one operand is true ,then the result is true.
  4. Example:

     if(a==1||b==1)
     System.out.println("Hi");
    

    In the above statement,If either of the a or b value becomes equal to 1.
    Hi will be displayed

! (NOT) operator

  1. ! (NOT) operator performs complement operation.
  2. It is a unary operators.It has only one operand.
  3. ! applicable only for boolean types and we can not apply for integral types.
  4. Example:

    if(!true)
      System.out.println("Hi");
    else
      System.out.println("Hello");
    

Bit wise Operators

  1. & (AND) Operator.
  2. | (OR) Operator.
  3. ^ (X-OR) Operator.
  4. ~ (Bit wise Complement) Operator.

& (AND) Operator

If both operands are true then it returns true.
Example:

true&true=true

| (OR) Operator

If at lest one operand is true ,then it returns true
Example:

true|false=true

^ (X-OR) Operator

If both operands are different then it returns true.

Example:

true^false=true

We can apply above operators even for integral data types.
Example:


public class Test {
  public static void main(String[] args) {
	System.out.println(4&5);
	System.out.println(4|5);
	System.out.println(4^5);
  }
}
/*
Output:
4
5
1
*/

We cannot apply above operators for floating point data types.
Example:

public class Test {
  public static void main(String[] args) {
	System.out.println(4.0&5.0);//Compile time error
  }
}

~(Bit Wise Complement Operator)

We cannot apply this operator for the boolean types and we can apply only for integral types.
Example:

public class Test {
  public static void main(String[] args) {
	System.out.println(~true);//Compile time error
	System.out.println(~4);//-5
  }
}
  1. &,|and ^ operators applicable for both boolean and integral
  2. ~ operator applicable for integral datatypes
  3. ! operator applicable for boolean types

Short-Circuit Operators

To improve performance of the system short-circuit operators have introduced,

$,| &&,||
Both the operands should always be evaluated Second operand evaluation is optional.If it is required, then only it will be evaluated
Performance is low Performance is high
These operators can be applicable for both boolean and integral data types These operators can be applicable only for boolean datatypes


Note:

X&&Y
y will be evaluated if x is true.
X||Y
y will be evaluated if x is false.

Example:

public class Test {
 public static void main(String[] args) {
	int x = 10;
	int y = 15;
	if (++x > 10 & ++y < 15) {
		++x;
	} else {
		++y;
	}
	System.out.println(x);
	System.out.println(y);
 }
}
/*
Output:
11
17
*/

Example:

public class Test {
	public static void main(String[] args) {
		int x = 10;
		if (x < 5 && (x / 0) > 20) {
			System.out.println("hello");
		} else {
			System.out.println("hai");
		}
	}
}
/*
Output:
hai
*/