Cheers!
boB
The Skye is the limit with Yo Jabba Jabba!
Cheers!
boB
Robert Stepp wrote:Thanks, Skye, for your thoughtful response. I especially liked your clarifying println examples. Thanks, also to Campbell. BUT!
I must not have expressed myself clearly. The heart of my question was:
"According to the operator precedence table the assignment operator has much lower precedence than either the prefix or postfix forms of the increment operator. "
Steve
Robert Stepp wrote:
I must not have expressed myself clearly. The heart of my question was:
"According to the operator precedence table the assignment operator has much lower precedence than either the prefix or postfix forms of the increment operator. "
I understand (and had seen) the FAQ. I understand and accept Skye's points. But this behavior seems to violate the operator precedence hierarchy. The increment operators are at the very top with highest precedence, which, in my mind, means that their operations should occur first; whereas, the assignment operator is at the very bottom, which, again, seems to me should be applied last. So strictly in terms of operator precedence it seems that in both code snippets ++ should occur first and y = should occur last. I accept what actually happens, but I am currently unable to reconcile the actual behavior with the operator precedence concept. Have I clarified what I am really asking?
Henry Wong wrote:
... "Precedence" or "Order of Evaluation" are *not* the same thing. Higher precedence doesn't mean done first.
Henry
Cheers!
boB
What is going on, goes back to the definition of these two different operators.
By defn: pre increment operator will increment the integer value and will evaluate to the updated value.
post increment operator will increment the integer value but will evaluate to the pre updated value.
and yes the ++ has a higher precedence than the =, and is evaluated prior to the assignment, but the two operators do different things.
Cheers!
boB
There are three kinds of actuaries: those who can count, and those who can't.
Robert Stepp wrote:
I thought the whole point of "precedence" was to determine the proper order of operations? If this is not the case, what is really going on here? What is the point of establishing an order of precedence among operators?
And yes, the behaviour of the −− operators is the same but with decreasing values.evaluate to different results
Campbell Ritchie wrote:There are places where it is correct coding to use i++ or i-- as parts of expressions, for example when assigning to array elements...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Robert Stepp wrote:
Henry Wong wrote:
... "Precedence" or "Order of Evaluation" are *not* the same thing. Higher precedence doesn't mean done first.
Henry
I thought the whole point of "precedence" was to determine the proper order of operations? If this is not the case, what is really going on here? What is the point of establishing an order of precedence among operators?
It is correct in the sense of the word defined by Tony Hoare, that it will maintain the class' invariants.Winston Gutkowski wrote: . . .
Hmmm. Not so sure I agree...at least with the word "correct".
. . .
Campbell Ritchie wrote:Yes, the operators were copied from C/C++, but their behaviour is not strictly defined in those languages. It is strictly defined in Java®.
Henry Wong wrote:In Java, order of evaluation is defined by the JLS, as mostly going from left to right (you have to read the JLS for exceptions).
Tedy Kwan wrote:It is easy to understand y=x++; but it is difficult to see y =y++; is the same as y=y;
I under stand Java evaluate left to right, so after the assignment; the +1 operation ... vanish?
Could some one explain that?
div tripathi wrote:You can check above code and run it accordingly
I think that is only part of the story. Another part of the story is that such code can be relied upon to confuse inexperienced programmers. And yet another part of the story is that there are (for a very short time) two values, that of x and that of the whole expression. Only in the case of x++ the two values are different, as MS said, and for ++x they are the same.Mark Spencers wrote:The only difference is that the prefix version (++x) evaluates to the incremented value, whereas the postfix version (x++) evaluates to the original value. . . .
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |