| Author |
Clarification on using increments
|
Keshan Pillay
Greenhorn
Joined: May 21, 2008
Posts: 29
|
|
That a fragment of code I had in an exercise recently. It gives the output: x:21 y:20 z:15 I can't seem to work out why this is. Could anyone please explain this to me?
|
 |
amitabh mehra
Ranch Hand
Joined: Dec 05, 2006
Posts: 98
|
|
x++, y++, z++... these are post increment operator [ May 22, 2008: Message edited by: amitabh mehra ] [ May 22, 2008: Message edited by: amitabh mehra ]
|
 |
amitabh mehra
Ranch Hand
Joined: Dec 05, 2006
Posts: 98
|
|
|
u can take a look PostIncrementOperatorAndAssignment
|
 |
Mala Gupta
Author
Ranch Hand
Joined: Sep 27, 2002
Posts: 196
|
|
Keshan Revisit the following lines of code: Line1> int x = 10; Line2> int y = 20; Line3> int z = 15; Line4> x = y++; // Values of x and y, AFTER the execution of this line of code: x=20, y=21 Line5> y = x++; // Values of y and x, AFTER the execution of this line of code: y=20, x=21 Line6> z = z++; // Value of x and y, AFTER the execution of this line of code: z=15 For lines 4 and 5, please note that when a postfix increment operator is used, the value of the variable is used before its value increments. For line 6, the value of z will not change. Please refer to the following link for more information on this topic: http://faq.javaranch.com/java/PostIncrementOperatorAndAssignment cheers Mala
|
Author of book OCA Java SE 7 Programmer I Certification Guide from Manning
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32666
|
|
Please use real words, not "u" Some people didn't grow up with English, or use translation software, and they cannot understand "u". UseRealWords [ May 22, 2008: Message edited by: Campbell Ritchie ]
|
 |
Keshan Pillay
Greenhorn
Joined: May 21, 2008
Posts: 29
|
|
|
Thank you very much for your help, I understand how it works now. ^_^
|
 |
 |
|
|
subject: Clarification on using increments
|
|
|