This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Q about '|| &&'

 
Naveed Ali
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry,
Forget the answer
I now see your point.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
bart zagers
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is the final conclusion form this?
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic