Re: Question about ++
- From: Patricia Shanahan <pats@xxxxxxx>
- Date: Mon, 09 Apr 2007 16:06:25 GMT
richnjones@xxxxxxxxx wrote:
Hello all,
I have got a question about the post increment operator. Consider the
following code
int i = 1;
System.out.println(i);
i = i++;
System.out.println(i);
The output is
1
1
I expect the first 1 because I am (for sanities sake) outputting the
value of i before any operations are done on it.
I then expect the value of i on the right of the = to be assigned to
the i on the left of the equals. I expect that to copy it and then the
increment to happen. I do not see why the increment is not done on teh
i to the left of the equals. I know this is with primitives whcih pass
by value but I was expected an output of
1
2
It seems int he code above the ++ is completly disregarded
If you really want to understand code like "i=i++;", you need to
understand the definition of postfix ++ in Java,
http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.15.1
There are a couple of significant aspects to every operation:
1. What result, if any, does it provide for use in subsequent calculations?
2. What side effects does it have?
For postfix ++, the result is the old value of the variable, and the
side effect is to increment the variable by 1.
Java completely evaluates the right hand side of an assignment before
performing the assignment. The sequence for i=i++; with i initially 1 is:
Determine the left hand side field - trivial because it is not an array
access or anything else that might have side effects.
Evaluate the right hand side
Calculate the result, 1
Perform the side effect, change i to 2
Do the assignment, setting i to the right hand side result, 1.
Of course, a smart optimizer might decide all this is a waste of time,
and do nothing, but the results will be the same as though this was all
done.
What made me think of this is another question in the forum
(http://groups.google.co.uk/group/comp.lang.java.programmer/
browse_thread/thread/
fb0d3fbd12040ba3/5153c4175a471b0a#5153c4175a471b0a)
asking the answer to
int a = 1;
int b = a++ + a++;
System.out.println("A = " + a);
System.out.println("B = " + b);
in C. I tried it in Java and the b=.... line seemed to be doing
operations left to right although Im not too clear with this either
The output is
A = 3
B = 3
Evaluate the right hand side
Evaluate the left operand of the binary +
Result is old value of a, 1
Side effect, change a to 2
Evaluate the right hand side of the +
Result is old value of a, 2
Side effect, change a to 3
Evaluate the binary +
Result is sum of the left and right hand side results, 3
Do the assignment, setting b to the right hand side result, 3
Can anyone clarify what is expected when ++ and assigning
My usual expectation when code combines ++ and assigning is very
confused, unreadable code that probably does not do what the programmer
intended. Even if done "correctly" in the sense of getting the intended
result, it is a warning sign of code that sacrifices clarity in favor of
unnecessary cleverness.
Patricia
.
- Follow-Ups:
- Re: Question about ++
- From: richnjones
- Re: Question about ++
- References:
- Question about ++
- From: richnjones
- Question about ++
- Prev by Date: Re: Am I Crazy? V. simple math question.
- Next by Date: Re: Question about ++
- Previous by thread: Re: Question about ++
- Next by thread: Re: Question about ++
- Index(es):
Relevant Pages
|