my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes postfix ++ and prefix ++ Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "postfix ++ and prefix ++" Watch "postfix ++ and prefix ++" New topic
Author

postfix ++ and prefix ++

Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
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

Joined: Jul 02, 2002
Posts: 1865
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<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jul 02, 2002
Posts: 1865
The output of the following version might be a little easier to understand.
Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
Thanks Barry for giving details of temporary variables actually used and showing temporary results...
It is really helpful
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Jul 02, 2002
Posts: 1865
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

Joined: Aug 26, 2001
Posts: 7610
Another good technique:
http://www.coderanch.com/t/190825/java-programmer-SCJP/certification/Array


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Aug 05, 2002
Posts: 787
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

Joined: Jun 06, 2002
Posts: 1056
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.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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
 
subject: postfix ++ and prefix ++