Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

# why i=i++; doesn't get incremented

Manwarjit Singh
Greenhorn
Posts: 6
HI,
In following code, Why does't value of i get incremented :
_______________________________________________
public static void main(String args[])
{
int i=9;
i=i++;
System.out.println(i);
}
}
_______________________________________________
output is : 9
Where as it should be 10.
-Thanks-

Manfred Leonhardt
Ranch Hand
Posts: 1492
Hi,
We know the post increment operator means to use the current value and then perform the increment. In you example:
1. Copy current value of i to memory location, M1: 9
2. Increment value that i is referencing: i --> 9+1 = 10
3. Perform assignment of M1 to i: 9
Result, i --> 9!
It even gets more interesting if you had written:
int i = 0;
i = i++ + i++;
Then we get:
1. Copy current value of i to memory location M1: 0
2. Increment current value of i: 0+1 = 1
3. Copy current value of i to memory location M2: 1
4. Increment current value of i: 1+1 = 2
5. Perform binary addition on M1 and M2: 0 + 1 = 1
Regards,
Manfred.

Gaurav Mantro
Ranch Hand
Posts: 61
Hi.
Can you help with how to resolve silly/confusing areithmatics like
b=a+++c-2++++d-++k
Que 1. What's the logic to resolve these computations using operator precedence?
Que 2. Do we get questions like this in Certification exams ?

------------------
http://www.mantrotech.com

Jane Griscti
Ranch Hand
Posts: 3141
Gaurav,
Read Maha Anna's answer to solving these type of questions in http://www.javaranch.com/ubb/Forum24/HTML/000775.html .
If you can solve these you won't have a problem on the exam, luckily, the questions aren't quite so convoluted.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Javaï¿½ 2 Platform

Manwarjit Singh
Greenhorn
Posts: 6
Thanks Manfred for clearing my dbout.
[This message has been edited by Manwarjit Singh (edited February 16, 2001).]
[This message has been edited by Manwarjit Singh (edited February 16, 2001).]

Wasim Ahmed
Ranch Hand
Posts: 90
Answer is yes, atleast one question. They want to know your understanding of Operator pre-increment /post-increment. Here is the easy method that I have learnt and always works.
int i = 1
i= i++ + i++ + i
put increment operator in inthe prenthesis.
i = 1(2) + 2(3) + 3
value of i changes when previou expression perform the increment operation but it doesn't get executed. now drop the values from the prenthesis.
i = 1 + 2 + 3 = 6
I hope this will help

Manwarjit Singh
Greenhorn
Posts: 6
Thanks Wasim, Jane
Similarly we can do for post/pre, increment/decrement :-
Here is the example :-
_____________________________________________
public class M{

public static void main(String args[])
{
int i=1;
i = i++ + ++i + i + --i + i--;

// i = 1(2) + (3)3 + 3 + (2)2 + 2(1)
// now remove all non-barckets .
// So we get
// i = 1+3+3+2+2
// i = 11;

System.out.println(i);
}
}
_____________________________________________
Output is 11.
[This message has been edited by Manwarjit Singh (edited February 16, 2001).]

Bhasin Sameer
Greenhorn
Posts: 4
hi
see I feel i++ means i=i+1;
applying logic as mentioned above i think the question should be really explained well.

Manfred Leonhardt
Ranch Hand
Posts: 1492
Hi Manwarjit,
It seems you still don't get it. My answer for i = i++ + i++ is correct. I even showed you step by step how it works! The program below will prove me right!

correct this time,
Manfred.

Manwarjit Singh
Greenhorn
Posts: 6
Originally posted by Manfred Leonhardt:
[B]Hi Manwarjit,
It seems you still don't get it. My answer for i = i++ + i++ is correct. I even showed you step by step how it works! The program below will prove me right!

Sorry, I was calculating with i = 1 , But urs is with i = 0;
Its altright, I withdrew my comment.
correct this time,
Manfred.[/B]