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 ..
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.
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
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.
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 ...
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 ]
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 ]
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.
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
Post by:autobot
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