This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a tricky question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a tricky question" Watch "a tricky question" New topic
Author

a tricky question

Senthil Nathan.s
Greenhorn

Joined: Jan 04, 2006
Posts: 11
eg:
int i=10;
i=++i;
i=i++;
System.out.println(i);


doubt:
I expect the answer to be 12 but the answer is 11.Can anyone explain me how this happens


Thanks and regards
S.Senthilnathan


with regards<br />s.senthilNathan<br />PH:9819194691
Krishnan Loganathan
Greenhorn

Joined: Apr 24, 2004
Posts: 23
Hi senthilnathan,

when you issue the command
i = i++;

first it will assign the old value of 'i' (ie., 11) to the i and then it will add 1 to the old value.

so, when you print the i, it will print 11 only.

you just modify the code slightly as follows and test

int j = i++;
System.out.println("the value of i ==> " + i);

now it will print 12. because you are assiging the old value of i to the seprate variable 'j'.

Thanks & Regards,
Loga
Senthil Nathan.s
Greenhorn

Joined: Jan 04, 2006
Posts: 11
Thanks krishnan for your reply but anyhow the contents of i must be incremented by one know .You mean that the contents of i wont get incremented at all ,is it so.......
Murali Mohan
Ranch Hand

Joined: Jan 09, 2006
Posts: 66
int i=10;
i=++i;//1
i=i++;//2

The result of the prefix or postfix increment expression is not a variable, but a value.
For prefix or postfix increment the value 1 is added to the value of the variable and the sum is stored back into the variable.
The value of the prefix increment expression is the value of the variable after the new value is stored.
The value of the postfix increment expression is the value of the variable before the new value is stored.


in line 1 ++i evaluates to value 11
value of expression=11 & i=11
i=value of expression
in line 2 i++ evaluates to value 11
value of expression=11 & i=12
i=value of expression
=> i is overwritten with value 11

I think that solves your problem.


Thanks,<br />Murali...
Balaji Sampath
Ranch Hand

Joined: Sep 30, 2005
Posts: 63
Excellent Explanation Murali..

Thanks
Balaji.S
Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

I have seen hunderds of threads on this particular topic.
Mostly, the explanation given is 'reverse-engineered'.
Interestingly, if you do the same in C, you'll get 12 as your answer.
C != Java, but when you read the definition of the post-increment operator, do you find any difference?
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"Murali,"

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your name here.

Thank you for your prompt attention, and enjoy the ranch!

-Marc


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Post-Increment Operator with Assignment


SCJP Tipline, etc.
Senthil Nathan.s
Greenhorn

Joined: Jan 04, 2006
Posts: 11
Thanks for your explanation Murali.I understood the concept very well.
Murali Mohan
Ranch Hand

Joined: Jan 09, 2006
Posts: 66
Thanks to all.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: a tricky question
 
Similar Threads
A Challenge!
All Vowels in an Integer
Scripting Invalid
Threads (from jwhiz)
The best puzzle(IIM Indore)