• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

post increment confusion

 
B Hayes
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the above clarification.
 
rinku jain
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi............every one

does its right that

i++ or ++i is equivalent to

i=i+1;

 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
 
Ikpefua Jacob-Obinyan
Ranch Hand
Posts: 394
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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' ??
 
Stephan van Hulst
Bartender
Posts: 6311
77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic