• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

++ question

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Snippet 1:

int i = 0;
i = i++;
System.out.println(i); // This prints 0

Snippet 2:

int i = 0;
i = i;
i++;
System.out.println(i); // This prints 1

According to the definition of ++ postfix, the value should be used first and then incremented. So, I interpreted that both snippets should produce the same result. This doesn't seem to be the case. Can someone please explain.
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the second snippet, don't you mean
i = ++i;
Sashi
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The big thing to know is that i++ is like a function/method call. It returns a value.

so

i = 0;
i = i++;

the i++ returns the previous value which is 0 and assings it to i. that is why it prints out 0 and not 1.

It is a Java gotcha.

Mark
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The explanation for i = i++ is as follows:
i++ is an expression, and as such, it has a value. The value of the expression "i++" is the value of i before the increment. If you did "j=i++" you'd expect j to be 10, right? Because you know that i++, as a post-increment operator, gets the value first and then increments.

So, the value of i++ is 10. Once the expression is evaluated to get that value, then the increment occurs. So now i is 11.

Finally after the i++ has done its bit, whatever is on the left side (in this case it happens to be i, but it would be the same if it were j), gets the value of the expression. No, that value isn't 11. It's 10. The value of i++ is i's value before the increment. It's 10.
 
RaviKumar Kumar
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Mark and Anuja,

Full marks for your explainations. Very helpful.

Thanks
Ravi
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still couldn't understand.. please help...

Now it prints 1

But why will it print 0 for i = i++;
I understand that i++ means:
1)first use it; in this case assign 0 to i and then
2)increment i by 1. The process of increment is done before printing.
But still it prints 0.....Why ?? Please explain...
[ December 19, 2005: Message edited by: Ravisekhar Kovuru ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those who are not still clear I am copying which is already somewhere on Javaranch.

Let's take a close look at what the line "i = i++;" does:

"i++" is evaluated. The value of "i++" is the value of i before the increment happens.
As part of the evaluation of "i++", i is incremented by one. Now i has the value of 1;
The assignment is executed. i is assigned the value of "i++", which is the value of i before the increment - that is, 0.
That is, "i = i++" roughly translates to

int oldValue = i;
i = i + 1;
i = oldValue;

With other words, it is a common misconception that the increment is happening last. The increment is executed immediately when the expression gets evaluated, and the value before the increment is remembered for future use inside the same statement.

I hope this 'll help

Uzma
 
Ravisekhar Kovuru
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Uzma..
Now I got it...
it is somewhat tricky...
 
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
From a bytecode perspective, it looks like this:
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 0;
i = i++ + i++ + i++ + i++;
System.out.println(i);

Output = 6.

Since it is post incrementer, assignment occurs before increment.

Step � 1 � The first i++ will execute like, I will be assigned to the value 0 and it gets incremented.
Step - 2 � Since in step 1 the I value is incremented, it will be 1 now. So once again I will be assigned and increments happens. So now I value will be 0+1=1.
Step � 3 � Same things happens, since an increment had happened the I value after step 2 will be 2. So 1+2 = 3.
Step � 4 � Same thing happens, so 3+3 = 6.
 
I have a knack for fixing things like this ... um ... sorry ... here is a consilitory tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic