Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Help understanding output with a++

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I'm having trouble understanding why the output of the following is 5 instead of 4:



I know that the postfix operators affect the value AFTER the assignment. But since the variable, a, IS the variable being decremented, shouldn't it be affected by the -- after the assignment of the value of 5? This is clearly not the case. Why not? What is going on here?
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Trey,

'a' here is a primitive and not a Object.

int a = 5; ---> a is given a value 5
a = a--; ---> a is assigned a value 5 and then the RHS does nothing...!

Can you try and guess what the oputput of this would be:



It would be 11..!! Evaluate RHS and give it to LHS. RHS would be 5+6 and 11 would be assigned to a!
 
Lakuma Yalamanchili
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have also found this thread on Sun's java forums. Look into it:

http://forums.sun.com/thread.jspa?messageID=1734569#1734569
Hope this gives a bit of clarification!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
x = a++; //increment after assignment
x = a--; //decrement after assignment

x = ++a; //increment before assignment
x = --a; //decrement before assignment
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ,Go through coreys scjp tipline where there are excellent articles about various topics including pre and postfix operators to get a clear understanding.It helped me!
[ October 02, 2008: Message edited by: sumi rankan ]
 
Trey Carroll
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Thanks for the feedback. I'm afraid that I haven't made my question clear or that the subtlety of this question has been overlooked.

I understand the difference between prefix and postfix increment/decrement operators. If the original code were replaced with this:



I would have no problems at all. However, my question is this:

If we think of a as a labeled 32 bit bucket, then the code:



brings up a strange situation.

So far, so good, but then, after the assignment is complete, the -- is applied to the value stored in a!

Am I missing something here? After the assignment the -- is supposed to decrement the value stored in a.

The only scenario I can come up with to understand this output is to hypothesize that the actual process here runs like this:

1) evaluate the current value of in a and store this for later use in the assignment.
2) allow the decrement operation to run
3) assign the previously stored value to variable.

Thus, the diagram for my conjecture would be:

Can anyone confirm this or offer an alternative explanation?
[ October 02, 2008: Message edited by: Trey Carroll ]
 
Trey Carroll
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Sumi

Corey's Tipline was exactly what I needed. (He confirms my hypothesis.)

Also, Thanks to you, Lakuma. Your example:

a = a++ + a;//a = 11



Shed light on the process too. It taught me that the last step is always the assignment so increments and decrements will never "write through" to the LHS.

Thank you,

Trey Carroll
[ October 02, 2008: Message edited by: Trey Carroll ]
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Guys,

I got the impression from atleast one post in here that the increment from a++ never gets writtent back to a, so I'm adding my comments.

Take note of the following two points from the JLS & Java's operator precedence
* 15.7.1 Evaluate Left-Hand Operand First - The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.
* 15.14.2 Postfix Increment Operator ++ - The value of the postfix increment expression is the value of the variable before the new value is stored
* Operator precedence - http://www.irixtech.com/java/tutorials/java-operator-precedence

So, for the example, from the original post "a = a--", a-- puts 5 as the value in the expression & makes the final value of a as 4 per JLS 15.14.2. And now, since assignment will always happen in the end the final value of a ends up being 5 although a was 4 after a--.

Another example to consider is "a = a++ + a". In this case, per JLS 15.7.1 left-hand operands need to be fully evaluated before any part of the right-hand can be evaluated. So we evaluate a++, we get 5 in the expression & a has the value 6. Now we evaluate the right-hand operand which is simply substituting the value of a i.e. 6. Finally, the assignment is performed & a ends up being 11.

Take a minute to have a look at the said JLS articles, the JLS may seem geeky at times but all the answers are there.
JLS 15.7.1
JLS 15.14.2


HTH
Ashish Hareet
[ October 03, 2008: Message edited by: Ashish Hareet ]
 
Sheriff
Posts: 9692
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ashish Hareet:
Guys,
So, for the example, from the original post "a = a--", a-- puts 5 as the value in the expression & makes the final value of a as 6 per JLS 15.14.2. And now, since assignment will always happen in the end the final value of a ends up being 5 although a was 6 after a--.



I think you wrote 6 instead of 4. if a is 5 and you do a--, it will not result in 6, it will result in 4. But your explanation was good....
 
Ashish Hareet
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Ankit, it should've been 4, I'll edit my post
 
Willie Smits can speak 40 languages. This tiny ad can speak only one:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic