File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

postfix ++ and prefix ++

 
Binu K Idicula
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ..
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The output of the following version might be a little easier to understand.
 
Binu K Idicula
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Binu K Idicula
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Barry for giving details of temporary variables actually used and showing temporary results...
It is really helpful
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another good technique:
http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array
 
Binu K Idicula
Ranch Hand
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barkat Mardhani
Ranch Hand
Posts: 787
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Ron Newman
Ranch Hand
Posts: 1056
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic