• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

please assist me with this code

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



The result comes out to be true,false,false because of short circuit evaluation of || operator...but if we go by precedence && has higher precedence than ||..
in that way && should be evaluated first and the answer should come out to be
true,true,true...
why isn't precedence rule followed here??
help required please !!
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the java language specification:

The Java programming language also guarantees that every operand of an operator (except the conditional operators &&, ||, and ? appears to be fully evaluated before any part of the operation itself is performed.



This means that operands are evaluated first, regardless of precedence.

From the java language specification (15.24):

At run time, the left-hand operand expression is evaluated first; if the result has type Boolean, it is subjected to unboxing conversion (�5.1.8); if the resulting value is true, the value of the conditional-or expression is true and the right-hand operand expression is not evaluated.



This means that during the evaluation of operands, which happens before any of the real work is done, the runtime determines whether to continue with the expression at all based on the outcome of the evaluation of the operand.

So in your example, after the left operand is evaluated, no more work is done.



http://java.sun.com/docs/books/jls/third_edition/html/expressions.html

edit: I'm having a bit of troubled time writing code that actually proves, that after operands have been evaluated, && actually takes precedence over ||.
[ July 03, 2008: Message edited by: Ronald Schild ]
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I am not able to understand this.
Isn't the evaluation of the expression would be like
first the Parentheses are evaluated from left to right.
Then &&
Then ||
Then =
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Neo Chap
code:
--------------------------------------------------------------------------------

boolean x = (d = true) || (e = true) && (f = true);

--------------------------------------------------------------------------------

Here first expressions are evaluated from left to right. So this code is trying to assign value to x. But as (d=true) is in brackets, value true is assigned to d first which was having default false. Then same true is assigned to x. Then || operator comes into execution. But as first condition is true, it doesn't bother to check right hand side expressions.

HTH
Phalguni
 
Neo Chap
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Phalguni


Neo Chap
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Phalguni Bhatt:
Hi Neo Chap
code:
--------------------------------------------------------------------------------

boolean x = (d = true) || (e = true) && (f = true);

--------------------------------------------------------------------------------

Here first expressions are evaluated from left to right. So this code is trying to assign value to x. But as (d=true) is in brackets, value true is assigned to d first which was having default false. Then same true is assigned to x. Then || operator comes into execution. But as first condition is true, it doesn't bother to check right hand side expressions.

HTH
Phalguni



So you're saying the value is assigned to x before the || operator is handled?

Also, I think the parentheses do not have the effect as you describe. The JLS garuantees that operands are first evaluated in left to right order.

Consider:



The output is:
1
2
3
true

The numbers prove the evaluation from left to right of operands. The true proves that x is assigned after the whole expression is worked through.
 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
--------------------------------------------------------------------------
So you're saying the value is assigned to x before the || operator is handled?
--------------------------------------------------------------------------

Yes Ronald I mean to say the same. Please correct me if I am wrong.
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everything of the right of the = is worked through first.


Hope that helps feel free to ask questions about it.
[ July 03, 2008: Message edited by: Ronald Schild ]
 
Phal Ach
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ronald. I was really thinking it otherwise.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still had a bit of confusion here.



Here Ronlad said true && false is never worked. But && is having higher precedence than ||. So it should be evaluated first. and then || should be evaluated.

Now consider this example:

int x = 4 + 2 - 8 / 2
Here first operator / is having higher precedence than + or -. So first expression becomes
x = 4 + 2 - 4
Now x = 2.

So in the same way, since && is haing higher precendence than ||, it should be evaluated. Some body please correct me if i am wrong.
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The && operator does have higher precedence than ||. However, whether the right side operand is evaluated depends on the result of the evaluation of the left hand operand. This is done before operator precedence rules apply.



As you'll notice when running this code, the outcome is

1
true

This is because MuddyMain.printNumber(true, 1) is evaluated first. Depending on the result, the runtime decides whether to continue with the rest of the statement at all. This takes place before precedence rules. If precedence rules were handled, you'd see 2, 3, 1 true, but this is not the case.

For


The runtime will evaluate the left operand of || first (all operands are first evaluated from left to right). The result is true, and that's where it ends here, the right hand operand (and stuff after that) is not evaluated.

Another example:



The output here is:

1
2
3
27

As you can see, all operands are first evaluated. This gives 1, 2, 3 in this order, eventhough * takes precedence over +. After that has been done, 2 + 5 * 5 is worked through, resulting in 27 instead of 35, because here operator precedence is in effect.

edit: can't figure out why the format of this post appears a bit wacky on my screen. Any questions are most welcome
edit2: ah, because code blocks aren't wrapped. Fixed.
[ July 04, 2008: Message edited by: Ronald Schild ]
 
gaurav madan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot everyone. i have understood the concept now...Operands will be evaluated first and due to the short circuit operation of || operator when it evaluates the left hand side to be true the rest of the evaluation on its right hand side is skipped..
thank you!
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic