Conditional operators return a true or a false value based on the state of the variables i.e. the operations using conditional operators are performed between the two boolean expressions.
Symbol  Name of the Operator 
&  AND 
&&  ConditionalAND 
  OR 
  ConditionalOR 
!  NOT 
? :  Ternary (shorthand for ifthenelse statement) 
I. AND (&) and ConditionalAND (&&) operators:
The AND (&) operator is similar to
the ConditionalAND operator (&&).
Both of its operands are of boolean type, even these operands may be boolean
expressions. Other Relational operators can also be used with these operators.
Lets use a Truth Table to know the status of an output
Op1 or Exp1  Op2 or Exp2  Result 
True  True  True 
False  False  False 
True  False  False 
False  True  False 
If we analyze the table we find that result
is always true only in case of first condition where both operands(or
expressions) are true. On the other hand result is always false
in other conditions.
Note that this table works alike for & and &&
operators.
In case of "&" operator, if both operands or expressions are true, the result is always true, otherwise it is false if either lefthand or righthand operand is false.
In case of "&&" operator, the result is also true, if both operands or expressions are
true.
But this operator evaluates only
the lefthand operand. It doesn't evaluate the righthand
operand if the lefthand operand is false then it will not jump to the
righthand operand to evaluate it, and will come out from that statement and
read the next statement. That's why this mechanism
is known as shortcircuiting. Consider the following statements where the
second expression returns false after evaluating only the lefthand
operand.
true
&& true = true; //
both operands are evaluated false && true = false; // only leftoperand is evaluated 
But the "&" operator always evaluates both of its operands whether the first operand is true or false. Consider the following statements where the second expression returns false after evaluating the righthand operand.
true
& true = true; //
both operands are true true & false = false; // both operands are evaluated 
II. OR ()and
ConditionalOR ()
operators
:
Likewise, the OR operator()
is similar to the ConditionalOR
operator ()
and returns true, if
one or another of its operand is true.
Lets use a Truth Table to know the status of an output that works alike for
"" and " " operators.
Op1 or Exp1  Op2 or Exp2  Result 
True  True  True 
False  False  False 
True  False  True 
False  True  True 
If we analyze the table then we find that, result is always false only if both operands or expression are false. On the other hand, result is always true in rest of the other conditions.
Still these exists a major difference in their mode of use:
The "" operator always evaluates both of its operands and returns true if one or other of its operand is true. Otherwise false if both the conditions are false. Consider the following statements where the second expression returns false after evaluating the righthand operand.
true  false =
true; //
left operand is true but both are evaluated false  false = false; //both operands are evaluated 
In case of "" the result is also true, if one of the both operands is true. Otherwise it evaluates to false if both operands are false. But this operator conditionally evaluates the righthand operand only if the lefthand operands is false. Like the ConditionalAND operator, this mechanism is also known as shortcircuiting. Consider the following statements where the first expression returns true after evaluating the righthand operand.
false  true =
true; // both operands are
evaluated true  false = true; // only leftoperand is evaluated 
III. NOT ("!") operator :
The NOT ("!") operator performs
the boolean NOT operation on a single operand or an expression. It checks the
boolean status of a current operand or expression and reverses the value of a
boolean expression i.e. if the current value of an operand or expression is true
then it reverses as false; but if the value of an operand or expression is false
then it reverses as true.
Consider the following example:
class BoolNotDemo {

Output of the Program:
C:\nisha>javac BoolNotDemo.java C:\nisha>java BoolNotDemo x is not greater than y : false y is not greater than x : true 
IV. ternary ("?:")
operator
Java supports another conditional operator that is
known as the ternary operator "?:"
and basically is used for an ifthenelse
as shorthand as
boolean expression ? operand1 : operand2; 
The "?:"
operator evaluates an expression
which may also be an operand and returns operand1 if the
expression is true; otherwise returns operand2, if the expression
is false. We can understand this thing with the help of a
diagram shown as:
If we analyze this diagram then we find that, operand1 is returned, if the expression is true; otherwise operand2 is returned in case of false expression.
Lets have an example implementing some Logical operators:
class ConditionalOperator {

Output of the Program:
C:\nisha>javac ConditionalOperator.java C:\nisha>java ConditionalOperator value of x is 5 value of y is greater than the value of x The returned value is 5 
Advertisements
Posted on: May 24, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles
Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms. Connect Me on Google+
Discuss: Conditional (Logical) Operators View All Comments
Post your Comment