File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Q about '||  &&' Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Q about Watch "Q about New topic
Author

Q about '|| &&'

Naveed Ali
Ranch Hand

Joined: Aug 30, 2002
Posts: 41
class A {
static boolean a;
static boolean b;
static boolean c;
public static void main (String[] args) {
boolean x = (a = true) || (b = true) && (c = true);
System.out.print(a + "," + b + "," + c);
}
}

Why it prints true,false,false? Operator '&&' is of higher precedence
than '||' so 'b' and 'c' should also be evaluated to true?
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
The || and && operators don't evaluate the right expression if the left one is evaluated to true (|| operator) or false (&& operator).
In these cases, the result value is straight forward (it isn't necessary to evaluate the right expression)
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Sorry,
Forget the answer
I now see your point.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I think the different operands in a expressi´┐Żn are always evaluated in the order their appear in the expression. Other point is when the operation is performed: that depends on operator precedence.
Example 1:
a + b * c
a evaluated
b evaluated
c evaluated
* performed (with c and b)
+ performed (with the former result and a)
Example 2:
a * b + c
a evaluated
b evaluated
* performed (with a and b)
c evaluated
+ performed (with the former result and c)
Example 3:
a || b && c
a evaluated: if false doesn't eval. the right expr
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Java respects operator precedence and parenthesis, but it parses the expression from left to right. Once the JVM realizes that the left operand of a conditional "or" operator has evaluated to true the JVM knows that it is required NOT to evaluate the rest of the expression.
Remember, the conditional "and" operator, for example, is often used to prevent the evaluation of the right hand operand if the left hand operand evaluates to false. The left hand operand expression of a conditional "and" operator is often used to verify that a reference is not null. The right hand operand then uses the reference to invoke a method on the object.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Hello Jose Francisco, I think it can be summarized like this:
In the absense of parentheses and &&, || operators when considering if the operation of a given operator is performed or not, we must note the precedence of the operators to its left and the precedence of those to its right. An operation is performed before the operators to its left if they has fewer precedence and before the operators to its right if the precedence of those is equal or fewer.


SCJP2. Please Indent your code using UBB Code
bart zagers
Ranch Hand

Joined: Feb 05, 2003
Posts: 234
Hi,
Naveed started from the following assumption:
Operator '&&' is of higher precedence
than '||' so 'b' and 'c' should also be evaluated to true?

But I always assumed that both '&&' and '||' have th e same precedence? (Which would make the given explanations irrelevant in this case (but not less interesting))
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
But I always assumed that both '&&' and '||' have the same precedence?
From Java Cheat Sheet, && has a higher precedence than ||
[ February 05, 2003: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
So what is the final conclusion form this?
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Sarma Lolla:
So what is the final conclusion form this?

Of course that question always produces the same response: the Java Language Specification always provides the final conclusion. In this case, please see Section 15.7, Evaluation Order. For more information on the conditional "and" operator, &&, please see Section 15.23.
Remember that Java always parses expressions from left to right and evaluates each operand of the expression from left to right. Java respects operator precedence and parenthesis so many of the operands will be temporarily saved on the operand stack until they are needed.
In this question, the left hand operand, (a = true), of the conditional "or" operator, ||, is evaluated and the result is true. According to the JLS, evaluation of a conditional "or" expression ends if the left hand operand is found to be true. The right hand operand is never even evaluated. Similarly, the rest of the expression is never evaluated.
Suppose that the left hand operand of the conditional "or" expression was false instead of true. If that were the case, then the right hand operand would be evaluated. In this case, the right hand operand of the conditional "or" operator, ||, is actually an expression with higher precedence, (b = true) && (c = true). The entire conditional "and" expression, (b = true) && (c = true), would be evaluated and the result would serve as the right hand operand of the earlier conditional "or" expression.
Although Java does respect operator precedence and parenthesis Java still parses expressions from left to right. The operand stack is used to save the intermediate results until they are needed.
[ February 05, 2003: Message edited by: Dan Chisholm ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Q about '|| &&'