• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

post increment confusion

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the above clarification.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi............every one

does its right that

i++ or ++i is equivalent to

i=i+1;

 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Also, read this faq...

https://coderanch.com/how-to/java/PostIncrementOperatorAndAssignment
 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic