Java2all.com  




Share This Topic -

 

OPERATOR

MEANING

EXPLANATION

EXAMPLE

RESULT

&

Logical AND

Produce true if both operands are also true otherwise false

True & false

false

|

Logical OR

Produce true if one of them operand is true,

otherwise false

True | false

true

^

Logical XOR (exclusive OR)

if exactly one operand is true, then the result

is true. Otherwise, the result is false

True ^ false

true

||

Short-circuit OR

Check below program

Check below program

-

&&

Short-circuit AND

It will not check the second condition if the first one is false

Check below program

-

!

Logical unary NOT

This sign is used for invert the result

!true

false

&=

AND assignment

This is a short sign of Produce true if both operands are also true otherwise false on the same operand

Check below program

-

|=

OR assignment

This is a short sign of Produce true if one of the operand is true otherwise false on the same operand

Check below program

-

^=

XOR assignment

This is a short sign of produce true if exactly one operand is true, otherwise, the result is false on the same operand

Check below program

-

 
EX.
 
 public class Boolean_LogicalDemo 
{
	public static void main(String[] args) 
	{
		System.out.println("..............Boolean Logical Operators.................");
		System.out.println("<---------Basic Boolean Logical Operators--------->");
		boolean a = false;	
		boolean b = true;
		boolean c = a | b;		//Logical AND operator
		boolean d = a & b;		//Logical OR operator
		boolean e = a ^ b;		//Logical XOR(exclusive OR) operator
		boolean f = (!a & b) | (a & !b);
		boolean g = !a;			//Logical unary NOT operator
		System.out.println(" a = " + a);
		System.out.println(" b = " + b);
		System.out.println("Boolean Logical OR : a | b = " + c);
		System.out.println("Boolean Logical AND : a & b = " + d);
		System.out.println("Boolean Logical XOR(exclusive OR) : a ^ b = " + e);
		System.out.println("(!a & b) | (a & !b) = " + f);
		System.out.println("!a = " + g);
		
		System.out.println();
		System.out.println("<---------Boolean Logical Assignment Operators--------->");
		System.out.println("Before apply assignment operator, the value of a = "+a);
		a &= b;		//
		System.out.println("After apply assignment operator : a &= b means a = a & b hence now the value of a : "+a);
		/*Same as you can check Boolean OR assignment,Boolean exclusive OR assignment,
		  Boolean Equal to and Boolean Not equal to */
	}
}

Output :
 
..............Boolean Logical Operators.................
<---------Basic Boolean Logical Operators--------->
 a = false
 b = true
Boolean Logical OR : a | b = true
Boolean Logical AND : a & b = false
Boolean Logical XOR(exclusive OR) : a ^ b = true
(!a & b) | (a & !b) = true
!a = true
 
<---------Boolean Logical Assignment Operators--------->
Before apply assignment operator, the value of a = false
After apply assignment operator : a &= b means a = a & b hence now the value of a : false

 

 public class Boolean_ShortCircuitDemo 
{
	public static void main(String[] args)
	{
		System.out.println("<---------Boolean Short-Circuit Operators--------->");
		int i = 10;
		int j = 20;
		
		if (i != 10 && ++j/i>0)
		{
			System.out.println("i = "+i);
			System.out.println("j = "+j);
		}
		System.out.println("In Short-circuit AND operator(&&): ");
		System.out.println("Not checked the second condition in IF-BLECK if first condition is false.........");
		System.out.println("So,the value of j = "+j);
	//Same as you can check Short-circuit OR operator(||)
	}
}

Output :
<---------Boolean Short-Circuit Operators--------->
In Short-circuit AND operator(&&): 
Not checked the second condition in IF-BLECK if first condition is false.........
So,the value of j = 20