This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
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.
Joined: Feb 04, 2001
Thanx Bill i got the funda. Appreciate your reply.
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