Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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

i = i++ ?!

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..am rather puzzled at what could be a possible explanation for this..

class Test
{
public static void main(String a[])
{
int i=1;
i = i++;
System.out.println(i); // prints 1 - shouldn't that be 2 ?!
}
}

Doesn't the incremented value of i gets stored back in i ?

Regards
Alags
 
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
there was a post on this just a bit ago.

use:
i++;

its exactly equal to
i = i + 1;
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It's not actually, and since it's puzzler week, I'll let you ponder.
 
Matt Fielder
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok how about.

i++ means use i then increment by one
as ++i is increment by one then use i

damn you. don't make me think. its been a long day.
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
bingo
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nup - keep thinking.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alagapan Thiagarajan:
Doesn't the incremented value of i gets stored back in i ?



Yes - but *before* the old value of i gets assigned to i by the assignement operator.

Remember, i++ will increment the value of i, but it's value will be i before the increment.
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


The value contained in the 'i' variable is incremented. However, the expression 'i++' evaluates to the pre-increment (original) value of 'i'. The '=' assignment operator has the least precedence, so it happens last: the pre-increment (original) value of 'i' is assigned back to 'i'.

Anyone who writes production code like this should be slapped with a mackerel.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are three types of people in the world. Those who can count, and those who can't.


I'm really curious about the third type, or, which is the third type?
 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>> I'm really curious about the third type, or, which is the third type?

Those who don't get it.
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rick O'Shay:
>> I'm really curious about the third type, or, which is the third type?

Those who don't get it.



LOL
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, I'm not getting much of it either, that makes meeting me a "Strange Encounter of the Third Kind".
 
Kuldeep Vaishnav
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
----------------------------------------
Nup - keep thinking.
----------------------------------------
why? is it not right ??
 
Saeed Amer
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I maybe wrong but this is what I think is happening:

i = i++;

Value of "i++" (on the right of the expression) is incremented/evaluated *after* being assigned to "i" (because it is post-increment) on the left side of expression (which is essentially the same as it was on the previous line). The result of "post-increment" to "i" (i++) is eventually discarded. The value of expression on the right side of "=" is evaluated in memory and then assigned to the variable on the left. Since "i" is to be "post-incremented" (i.e., after the value has been assigned to the variable on the left), it is discarded.

Hence, "1" is printed.

Saeed

[ August 11, 2005: Message edited by: Saeed Amer ]
[ August 11, 2005: Message edited by: Saeed Amer ]
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To reiterate, the expression "i++" evaluates to the original value of "i". In truth, whether or not "i" is actually incremented is pretty much irrelevant, as it's the result of the expression "i++" that gets assigned to the "i" variable in "i = i++;".

I guess I'm up to my $0.04, now...
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
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