Java : Boolean Logical Operators


The Boolean logical operators operate only with Boolean Operands (true or false) where combine results of expressions will be boolean after performing two or more boolean logical operators.

Here is a complete list of Boolean Logical Operators:

Operator Result
& Logical AND
| Logical OR
^ Logical XOR (exclusive OR)
|| Short-circuit OR
&& Short-circuit AND
! Logical unary NOT
&= AND assignment
|= OR assignment
^= XOR assignment
== Equal to
!= Not equal to
?: Ternary if-then-else

Table to the effect of each operator

Here is a list of results when combining boolean operators’ operands values.

A B A | B A & B A ^ B !A
False False False False False True
True False True False True False
False True True False True True
True True True True False False

See Also :

Boolean Logical Operators Example

public class BooleanLogicalOperatorTest {

		    public static void main(String args[])
		    {

		        boolean a = false;
		        boolean b = true;

		        boolean c = a | b;
		        boolean d = a & b;
		        boolean e = a ^ b;
		        boolean f = (!a & b) | (a & !b);
		        boolean g = !a;

		        System.out.println("            a = " + a);
		        System.out.println("            b = " + b);
		        System.out.println("          a|b = " + c);
		        System.out.println("          a&b = " + d);
		        System.out.println("          a^b = " + e);
		        System.out.println("(!a&b)|(a&!b) = " + f);
		        System.out.println("           !a = " + g);

	

Output



            a = false
            b = true
          a|b = true
          a&b = false
          a^b = true
(!a&b)|(a&!b) = true
           !a = truecode>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s