Operator problem

Amit Trivedi
Greenhorn
Posts: 25
check this
int i = 9 ;
i = i++ ;
System.out.println(i) ;
output = 9
int i = 9 ;
i = ++i ;
System.out.println(i) ;

output = 10 Why?
Can we expact questions on these topics
- code in a html file for Applet
Mock exam by JohnJunt is covering above topics. Please guide.

Bill Compton
Ranch Hand
Posts: 186
The answer to the operator part of your question lies in the sequence of events. With i = i++; I believe the sequence is:
1) Determine the value of the expression "i++", which is 9.
2) Increment i, making it 10.
3) Store the value of the expression (9) into i, putting it back to 9.
However, with i = ++i; the sequence is:
1) Increment i to 10.
2) Determine the value of the expression: 10.
3) Store the value into i, making it 10.
Pre-increment vs. post-increment (and -decrement) is an important distinction. Think of it this way: pre-increment first increments the value, then yields that new value, whereas post-increment yields as its value the old value and then "afterward" increments the variable.

Amit Trivedi
Greenhorn
Posts: 25
Thanx Bill

Golam Newaz
Ranch Hand
Posts: 64
Hi,
Amit i think this one we can explain in very easy way. Look it is very important when you get job in any company if employer
finds that you are making mistake in this code increment
you will be fired immediately from the job.
check this
int i = 9 ;
i = i++ ;
System.out.println(i) ;
output = 9
int i = 9 ;
i = ++i ;
System.out.println(i) ;

output = 10 Why?
Bcoz , i=i++ means i first assign its value to i then
it will start increments. So i==9

Second i=++i means i first be incremented then it will
assign its value to i. So i==10
Got it,
- Golam Newaz