| Author |
post increment confusion
|
B Hayes
Ranch Hand
Joined: Feb 07, 2003
Posts: 61
|
|
|
Thanks for the above clarification.
|
 |
rinku jain
Greenhorn
Joined: Jan 04, 2011
Posts: 26
|
|
hi............every one
does its right that
i++ or ++i is equivalent to
i=i+1;
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
Not exactly. ++i is equivalent to i = (T) (i + 1), where T is the type of i.
If i is a byte, then ++i means the same as i = (byte) (i + 1).
|
 |
rinku jain
Greenhorn
Joined: Jan 04, 2011
Posts: 26
|
|
so stephan
if i am taking i as byte and intial value of i as 127
then if i do ++i
then output should be -128
what do you think???
|
 |
rinku jain
Greenhorn
Joined: Jan 04, 2011
Posts: 26
|
|
one more question
if i=22;
and i=i++;
now i use system.out.println(i);
then why the output is 22 instead of 23.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
rinku jain wrote:so stephan
if i am taking i as byte and intial value of i as 127
then if i do ++i
then output should be -128
what do you think???
Yes, this is correct.
one more question
if i=22;
and i=i++;
now i use system.out.println(i);
then why the output is 22 instead of 23.
This is because the post increment operator returns the value of the variable before it was incremented. If you reassign that value back to the variable using the assignment operator, you essentially make the increment useless.
|
 |
rinku jain
Greenhorn
Joined: Jan 04, 2011
Posts: 26
|
|
I think you are not getting what i am trying to say
once again
first we are initializing i with 22
then post incremented
i=i++;
now what happening here is that there is common memory of i
so if I post increment i, assign it to i i.e i=i++
what would be in i will be 22
but after assigning i is incremented i++ i.e actually means i=i+1;
so actually the output should be 23 instead of 22.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
rinku jain wrote:
now what happening here is that there is common memory of i
so if I post increment i, assign it to i i.e i=i++
what would be in i will be 22
but after assigning i is incremented i++ i.e actually means i=i+1;
so actually the output should be 23 instead of 22.
Actually, no. Post increment doesn't mean to increment after the expression is completely evaluated. Post increment means to increment the variable after it is used in the expression. Now, whether the increment happens first or whether the assignment happens first depends on the evaluation order. And the order of evaluation requires that the right side be evaluated, and hence, i gets incremented, before the assignment takes place.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
Also, read this faq...
http://www.coderanch.com/how-to/java/PostIncrementOperatorAndAssignment
|
 |
Ikpefua Jacob-Obinyan
Ranch Hand
Joined: Aug 31, 2010
Posts: 394
|
|
Marlene Miller wrote:The value of the expression is a reference to an object of type C.
Hello Marlene, I have some doubts about the above statement, is the value of the expression 'a-reference-to-an-object' or 'an-object' ??
|
OCPJP 6.
In Your Pursuit Towards Certification, NEVER Give Up.
|
 |
Stephan van Hulst
Bartender
Joined: Sep 20, 2010
Posts: 3044
|
|
Since Marlene posted that in 2004, I will take the liberty of answering that for her.
The value of the expression is a reference to an object. In Java, we can never work with objects directly. The dot (.) operator first always dereferences a reference before an operation is performed on the object.
|
 |
 |
|
|
subject: post increment confusion
|
|
|