Forums Register Login

postfix ++ and prefix ++

+Pie Number of slices to send: Send
can anybody tell me how is value of a variable affected when operator precedence is applied for the same variable in a single expression ..
pls see this ,

int a = 1;
a = ++a + a++;
System.out.println(a);
prints 4
int a = 1;
a += ++a + a++;
System.out.println(a);
prints 5
what I thought was
a = (++a) + (a++)
since a++ executes first , value of a is 2 , but it returns 1. So when ++a is calculated , it should return 3 (already a is 2).
So a = 4; Is this the correct explanation .right ??
If so , a += ++a + a++ ie a = a + ++a + a++, should be a = 3 + 3 + 1 ??
7 is it not ??
sorry if my doubt is confusing or unclear ..
+Pie Number of slices to send: Send
 

Originally posted by Binu K Idicula:

a = (++a) + (a++)
since a++ executes first , value of a is 2 , but it returns 1. So when ++a is calculated , it should return 3 (already a is 2).
So a = 4; Is this the correct explanation .right ??


Although the postfix operator has higher precedence than the prefix operator, Java
always evaluates the left operand of a binary
operator first.
+Pie Number of slices to send: Send
The following program demonstrates how Java evaluates expressions with unary operators.

The output is 1,1,1,2,2.
Java Language Specification, Section 15.7, Evaluation Order.
+Pie Number of slices to send: Send
The output of the following version might be a little easier to understand.
+Pie Number of slices to send: Send
Dan Chisholm ,
Thanks for that .. so
a = ++a + a++ , evaluates to 4 as
a = 2 + 2 ; right ?
so after a++ is executed , value of a is 3 Am I right ? is it overwritten as 4 ?
when using compound assignment ,
a += ++a + a++ ;
should evaluate to a = 3 + (4) ..= 7 is it ?
This question I saw from your mock exam .. Good questions .. really good. And thanx for that
+Pie Number of slices to send: Send
Binu, you are almost there:
Look at x += (++x) + (x++); // was x-- !
as being like this:

That's why 5 is printed.
--Barry
[ August 12, 2002: Message edited by: Barry Gaunt ]
+Pie Number of slices to send: Send
Thanks Barry for giving details of temporary variables actually used and showing temporary results...
It is really helpful
+Pie Number of slices to send: Send
 

Originally posted by Binu K Idicula:
Dan Chisholm ,
Thanks for that .. so
a = ++a + a++ , evaluates to 4 as
a = 2 + 2 ; right ?
so after a++ is executed , value of a is 3 Am I right ? is it overwritten as 4 ?
when using compound assignment ,
a += ++a + a++ ;
should evaluate to a = 3 + (4) ..= 7 is it ?
This question I saw from your mock exam .. Good questions .. really good. And thanx for that


Binu,
The following quotation is from section 15.26.2 of the Java Language Specification.


A compound assignment expression of the form E1 op= E2 is equivalent to E1 = (T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.


The following statements
int a = 1;
a += ++a + a++
can be rewritten as follows.
a = (int)((a) + (++a + a++));
If we substitute the initial value of "a", then we have the following.
a = (int)((1) + (++a + a++));
Java always evaluates the left operand of a binary operator first. In this case, the left operand is one.
The right operand is the expression (++a + a++). Once again, the left operand is evaluated first. In this case, the left operand is the prefix expression ++a. The value of variable "a" is incremented to 2 and the result is the value that is used as the left operand. The right operand is the postfix expression a++. The value used as the right operand of the addition operator is 2. The value of "a" is then incremented to 3, but the value is thrown away.
Both the left and right operands of the addition operator are 2. The result of the addition is 4. That result becomes the right operand of the earlier addition operator. The left operand is 1 and the right operand is 4, so the result is 5.
+Pie Number of slices to send: Send
Let me add one more comment to my previous post.

int a = 1;
a += ++a + a++
can be rewritten as follows.
a = (int)((a) + (++a + a++));
or

a = (int)((1) + (2 + 2));
or
a = 5;
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
Now it is clear ..
I hope it is better to expand the compound assignment operation before doing anything on it
as
a = (int)(a + (++a + a++))..
So that we can substitute the values easily in that equation..
Thanks again ...
+Pie Number of slices to send: Send
Binu,
Be aware that Dan's explanations are more technically correct than mine, read the Java Language Specification very carefully, just a little bit at a time.
You will have to iterate several times until the AH HA feeling comes, but it will.
-Barry
[ August 12, 2002: Message edited by: Barry Gaunt ]
+Pie Number of slices to send: Send
I am new to Java. I kind of started to believe
Bruce Eckel's point in Thinking in Java that Java
is about increasing programmers' productivity. I am coming
from Microsoft world. I do not understand why we
need such compliciated specification as
demonstranted in this example. What is the
pratical untility for such features (I was about
to say, complexities). Do not take me wrong. I
still love java.
Thanks
Barkat
[ August 12, 2002: Message edited by: Barkat Mardhani ]
+Pie Number of slices to send: Send
In C or C++, expressions like these would be considered undefined or implementation-dependent. For some reason, Java decided to define their semantics exactly, but they're still a bad idea in real life.
+Pie Number of slices to send: Send
Begin Thinks: these things are looking complex because we are dipping our toes into the deeper waters of Computer Science; there's a Gru around, your sword is glowing...
End Thinks.
-Barry
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1243 times.
Similar Threads
A simple question i=i++ ?!
Displaying output for the given format
can anybody explain this??
can anyone explain this...
Why its not incrementing???
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 01:33:00.