class Test { public static void main(String args[]) { int i = 1; i = i++; System.out.println(i); } } ----------------------------------------------- Output: 1 ------------------------------------------------ Any clues on why output is not 2, when i has been incremented ?
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
posted
0
Originally posted by Anupreet Arora: class Test { public static void main(String args[]) { int i = 1; i = i++; System.out.println(i); } } ----------------------------------------------- Output: 1 ------------------------------------------------ Any clues on why output is not 2, when i has been incremented ?
Because the postfix operators (++ and --) return the value before incrementing. So the final effect is to dicard the intermediate result of i+1 and set i equal to whatever it was before incrementing. Try the prefix operator instead:
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Enamul Haque
Greenhorn
Joined: Jun 07, 2003
Posts: 21
posted
0
so,
i++; is equavalant to i=i+1; so i = i++; means i = i = i+1; isn't it
<b><i>We all are the components of a huge program...... the programmer is always debugging us with His debugger.</b></i>
Michael Morris
Ranch Hand
Joined: Jan 30, 2002
Posts: 3451
posted
0
Originally posted by Enamul Haque: so,
i++; is equavalant to i=i+1; so i = i++; means i = i = i+1; isn't it
It's not equivalent. The pre/postfix operators (++ and --) are unary. All of the arithmetic operators (+, -, * etc.) are binary. Binary in the sence that it requires two operands. The statement i=i+1 as you probably already know is programmer shorthand for: Grab the contents of i Add 1 Return the sum Store the sum at i The term i+1 has absolutetly no effect on i. But the unary operators do affect their operand. i++ means: Grab the contents of i Add 1 Store the sum at i Return i (original value) For ++i: Grab the contents of i Add 1 Store the sum at i Return the sum So the effect of i=i++ is to temporarily store i+1 at i but then when it returns the original value, that is stored at i so no final change takes place. [ June 30, 2003: Message edited by: Michael Morris ]
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
To simplify what Michael is saying, the postfix operator returns the value of i prior to incrementing it. So if i is equal to 0, when you do i=i++ you get this: A) return 0 B) increment i to be equal to 1 C) take the returned value from step A and assign it to i You have to remember that the postfix operator has a higher operator precedence than assignment so that is why the order is as above.
Another Way of writing your program is class Test { public static void main(String args[]) { int i = 1; //i = i++; This line is commented.
System.out.println(i++); } } Since i++ is what is called as a "post- increment" operator, the compiler allows the operation following the increment statement to be performed and then does the incrementing. So in your case, first the present value of "i" which is 1 is displayed and then the increment operation is done. Hope this helps.
Anupreet Arora
Ranch Hand
Joined: Jun 17, 2003
Posts: 81
posted
0
Thanks folks.. Things are much clearer now. Especially, the question by Haque, which triggered the detailed clarification.. Cheers Anupreet