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

# A simple question i=i++ ?!

Greenhorn
Posts: 1
• Number of slices to send:
Optional 'thank-you' note:
Hi, everyone. I have a question which I dont understand.
1. int i=0;
2. i=i++;
3. System.out.println(i); //print out 0
I thought the result should be 1; as the i in line 2 is assigned to 0, then post ++ increases the i from 0 to 1. However, the program prints out a 0. May anyone explain to me why it turns out like this, please?

Raymond

mister krabs
Posts: 13974
• Number of slices to send:
Optional 'thank-you' note:

Ranch Hand
Posts: 284
• Number of slices to send:
Optional 'thank-you' note:
here is a bit that i found helpul on that thread - posted by Wasim Ahmed,
"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 parenthesis.
i = 1(2) + 2(3) + 3
value of i changes when previous expression performs the increment operation but it doesn't get executed. now drop the values from the parenthesis.
i = 1 + 2 + 3 = 6
"
I like the idea about using the parenthesis.
But i still don't understand how, although ... hold on ... oh okay, i get it ... i think, the post increment indicates that 'i' is equal to 'i' and then it 'would' be equal to the incremented version of 'i' if there is another arithmetic thing going on ... but seeing in the initial example 'i' is printed out in the next line, it does not get incremented?

Ranch Hand
Posts: 284
• Number of slices to send:
Optional 'thank-you' note:
the thing is though, the following code will print out 1.
class Increment {
public static void main (String[] args) {
int i=0;
i++;
System.out.println(i);
}
}

Author & Gold Digger
Posts: 7617
6
• Number of slices to send:
Optional 'thank-you' note:
Also take a look here:
https://coderanch.com/t/190825/java-programmer-SCJP/certification/Array
https://coderanch.com/t/239576/java-programmer-SCJP/certification/result
[ January 23, 2003: Message edited by: Valentin Crettaz ]

Ranch Hand
Posts: 71
• Number of slices to send:
Optional 'thank-you' note:

1. int i=0;
2. i=i++;
3. System.out.println(i); //print out 0

i is initialized to 0 before it gets incremented to 1.
--------------
Nayan

Greenhorn
Posts: 23
• Number of slices to send:
Optional 'thank-you' note:

When using the post-increment/post-decrement operators, the increment/decrement is applied to the value of the variable AFTER the operation is performed. Hence the prefix 'post'.
Line 1: variable i is initialized to zero.
Line 2: variable i is printed BEFORE the statement finishes execution, so it prints 0, then the value of i is incremented by 1, to 1.
Hope this helps some. This type of stuff can be confusing, I know.

 Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters?