aspose file tools*
The moose likes Beginning Java and the fly likes ++X Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "++X" Watch "++X" New topic
Author

++X

frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Khalid Mughal states that the prefix unary operator associates right to left. Can anyone construct an expression where this will happen ? I've tried for days with no luck...
3-13-01
I think I found what I was looking for:
-~++x would associate like this -(~(++x)) which would be right associativity and post fix would associate like this ((x++)~)- which all seems to make sense.
[This message has been edited by herb slocomb (edited March 13, 2001).]
Andy Ceponis
Ranch Hand

Joined: Dec 20, 2000
Posts: 782
Im not sure exactly what your asking here. Can you elaborate?
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
That's the problem, Khalid makes the statement I gave above, but doesn't elaborate or give any examples. Associativity rules are applied when there are 2 operators of the same precedence that follow each other in an expression. For example, "*" and "/" operators have equal precedence :
int x = 1,y = 2;
int z = y * x / y;
Associativity rules say "*" and "/" have left associativity, so they would be grouped left to right like this :
int z = ((y * x) / y);
The order is important because a different associativity would give a different result (in this case because they are int).
Now, can anyone on the planet give an example of right associativity for the prefix increment operator like I gave for the "*" and "/" operator above???

[This message has been edited by herb slocomb (edited March 12, 2001).]
Grant Crofton
Ranch Hand

Joined: Nov 08, 2000
Posts: 154
If you consider that the operator is '++' then the prefix version does have right associativity, as it evaluates the variable then increments it (applies the operator).
I'm not even sure if you could call it associativity, as there is only one side to evaluate (didn't really concentrate in maths)- so how do you show this in an expression? Perhaps the author's just trying to explain the difference between x++ and ++x.
I guess any expression shows it
x=1;
y=3*++x; // shows x is incremented before being multiplied
Siva Prasad
Ranch Hand

Joined: Feb 22, 2001
Posts: 104
Herb
You've asked this before many times in this forum
Jyotsna Clarkin has given very good explanation about this.
Can you go through that again?
John M. Gabriele
Ranch Hand

Joined: Feb 18, 2001
Posts: 232
you know what's funny herb?
van der linden (Just Java, p.123) states that pre-
increment/decrement has higher precedence than post-incr/decr.
flanagan (nutshell, p.30) says it's the other way around.
turns out in practice it doesn't matter.
btw, as you know, things like ++i-- and ++++i won't even
compile anyway.
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Thanks to everyone for their answers. I understand quite well how the pre and postfix operators work and their precedence order. My question is specifically only figuring out what the author meant when he said prefix operator "++" would have right associativity.

This has never been aswered in a way that makes sense. Associativity means you are grouping operand(s) and an operator together in a specific order to evaluate them as I did at previously in this post. Khalid distinguishes between post and pre fix operators as having different associativity - but he gives no examples. I suspect he gave no examples because there are none.

A prior response gave this as an example :
y=3*++x; // shows x is incremented before being multiplied
but Khalid also states that Associativity applies only to operators of equal precedence and that does not occur in the example above. There is also confusion between evaluation of the operand and application of the operator : they are two different things with two distinct steps.
Siva,
if you believe the question has been amswered please help me and print 1 single statement showing both left and right associativity using groups of pre and post fix operators. $5 dollars says you can't do it...
frank davis
Ranch Hand

Joined: Feb 12, 2001
Posts: 1479
Is there some way to close this post and reopen in the Intermediate level postings? No one here can answer the question it seems..
3-13-01
I think I found what I was looking for:
-~++x would associate like this -(~(++x)) which would be right associativity and post fix would associate like this ((x++)~)- which all seems to make sense.
[This message has been edited by herb slocomb (edited March 13, 2001).]
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Done.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ++X