aspose file tools*
The moose likes Beginning Java and the fly likes Clarification on using increments Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Clarification on using increments" Watch "Clarification on using increments" New topic
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: 245
    
    9
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 Manning's OCA Java SE 7 Programmer I Certification Guide and OCP Java SE 7 Programmer II Certification Guide
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36592
    
  16
Originally posted by amitabh mehra:
u can take a look PostIncrementOperatorAndAssignment
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. ^_^
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Clarification on using increments
 
Similar Threads
+ - operation
The "--" and "-" operators
x++ , ++x
ques
how to compare the values in arrays