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 precedence... 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 "precedence..." Watch "precedence..." New topic
Author

precedence...

Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
int i=0;boolean b;
b = (4 == 2 && ++i <= 1);
result : b is false.
here the first expn evaluates to false & so the second expn won't even be evaluated.
i have read earlier that all the operands are evaluated from left to right except for &&, ||, ?:.
Since, the relation operator which has higher precedence must be evaluated than conditional operator (&&)?
Please explain..


Anand<br />SCJP 1.4, SCWCD 1.4, SCEA 5.0(1/3)
Gian Franco
blacksmith
Ranch Hand

Joined: Dec 16, 2003
Posts: 977
Hi,
I've read that the operator && is
evaluated from left to right.
The only operators that are evaluated
from right to left are:
++ -- (prefix increment, prefix decrement)
++ -- (postfix increment, postfix decrement)
? : (conditional)
= += -= *= /= %= (assignment)


"Eppur si muove!"
Anand Ko
Ranch Hand

Joined: Dec 03, 2003
Posts: 79
I think u are talking about the "ASSOCIATIVITY". But I am interested in "PRECEDENCE"?
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Hi Annad,
The '&&' operator is evaluated from Left-to-Right, see the code below.



int i=0;boolean b;
b = (4 == 2 && ++i <= 1);
result : b is false.


The order of procedence is as follows:
1) ++i = 1 --> b = (4 == 2 && 1 <= 1)
2) <= --> b = (4 == 2 && true)
3) == --> b = (false && true)
4) && --> b = (false)
Hope this helps.


- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!
venkatesh rajmendram
Ranch Hand

Joined: Dec 05, 2000
Posts: 130


int i=0;boolean b;
b = (4 == 2 && ++i <= 1);
result : b is false.
The order of procedence is as follows:
1) ++i = 1 --> b = (4 == 2 && 1 <= 1)
2) <= --> b = (4 == 2 && true)
3) == --> b = (false && true)
4) && --> b = (false)

Hi, The above procedure might not be right...
b = (4 == 2 && ++i <= 1);
Here we have short circuit && , so the right side is not even evaluated i.e i never gets incremented....so
b = (false && ++i <=1);
b = false;
You can display the value of i before and after the execution it will remain unchanged...
- Venkatesh


SCJP 1.4, SCWCD<p>Ours is a world where people don't know what they want and are willing to go through hell to get it.<br /> - Don Marquis
Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522
Originally posted by venkatesh rajmendram:

Hi, The above procedure might not be right...
b = (4 == 2 && ++i <= 1);
Here we have short circuit && , so the right side is not even evaluated i.e i never gets incremented....so
b = (false && ++i <=1);
b = false;
You can display the value of i before and after the execution it will remain unchanged...
- Venkatesh

hmmm, good catch venkatesh, you are correct with your assumption, the RHS is never evaluated, i should've seen that . But anyways thanks for correcting me.
Ranchers, please disregard my previous post.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: precedence...