Showing posts with label Boolean Logical Operators. Show all posts
Showing posts with label Boolean Logical Operators. Show all posts

Thursday, 28 May 2015

Java Boolean Logical Operators

The Java supports the following boolean logical operators:

! (logical complement)

| (logical OR)

& (logical AND)

^ (logical exclusive OR)

Logical Complement ( ! ) Operator

This is a unary operator and the operand must always be a boolean expression. This acts as the negation operator, which negates a boolean expression as shown in the following table:

Boolean Expression(b) Logical Complement(!b)
true false
false true

Logical OR ( | ) Operator

The logical OR operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical OR operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 | b2)
false false false
false true true
true false true
true true true

If first boolean expression is true then value of the compound expression will be true irrespective of the value of the second boolean expression. Even then the second boolean expression is always evaluated. This is different from the conditional (short-circuit) OR operator where the second boolean expression is not evaluated if the result of the first boolean expression is true.

If both the operands of the | operator are of integer type then this operator behaves like bit-wise OR operator.

Logical AND ( & ) Operator

The logical AND operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical AND operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 & b2)
false false false
false true false
true false false
true true true

If first boolean expression is false then value of the compound expression will be false irrespective of the value of the second boolean expression. Even then the second boolean expression is always evaluated. This is different from the conditional (short-circuit) AND operator where the second boolean expression is not evaluated if the result of the first boolean expression is false.

If both the operands of the & operator are of integer type then this operator behaves like bit-wise AND operator.

Logical Exclusive OR or XOR ( ^ ) Operator

The logical exclusive OR operator is used to combine two boolean expressions to form a compound boolean expression. The value of any boolean expression in Java is either true or false no matter how complex it is. The following table shows the value of boolean expression obtained by combining two boolean expressions using logical exclusive operator.

Boolean Expression(b1) Logical Complement(b2) Logical Complement(b1 ^ b2)
false false false
false true true
true false true
true true false

If both the operands of the ^ operator are of integer type then this operator behaves like bit-wise exclusive OR operator.

Boolean Logical Assignment Operators

The Java supports the following Boolean Logical Assignment Operators:

&=, |=, ^=

An assignment operator has the following syntax:

<variable><operator> = <expression>

The above assignment is equivalent to:

<variable> = <variable>  <operator> (<expression>)

For example the assignment:

 b &= b1 is equivalent to b = b & b1

Here b is a boolean variable and b1 is a boolean expression