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
• Liutauras Vilda
• Tim Cooke
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Devaka Cooray
• Ron McLeod
• paul wheaton
Saloon Keepers:
• Tim Moores
• Piet Souris
• Tim Holloway
• Stephan van Hulst
• Carey Brown
Bartenders:
• Al Hobbs
• Frits Walraven
• Scott Selikoff

# Java - Increment operator

Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
I expected the value for m as 2, but it shows 1. Can any one clarify

Ranch Hand
Posts: 134
• Number of slices to send:
Optional 'thank-you' note:
after second INCRM operator if you do like n = m; then it will print n = 2;

m = ==m; //first incr so == 1;
m = m++ // first assignment after that increment

- cR

Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
The key thing to remember about the preincrement and postincrement operators is that when they are used as expressions, there is a difference in their values.

A preincrement, when used as an expression, has a value equal to the result of the increment.

A postincrement, when used as an expression, has a value equal to the value of the operand before the increment.

So if m = 1, then m++ has the value 1, but ++m has the value 2.

Note that both increments still add 1 to the value of m.

In your example, however, you have

m = m++

This essentially rolls back the increment since the assignment takes place after the increment.

Kumaran Pillai
Greenhorn
Posts: 7
• Number of slices to send:
Optional 'thank-you' note:
Thanks Keith for your brief explanation.

But my doubt is : In the line m = m++ , as you said, the value of M ( which is 1 because of previous increment) will be assigned to M and after it gets incremented as per the Post increment rule. But when i try to print the value of M, instead of giving 2, it is giving the output as 1. Will the value alone be assigned to M and the increment will happen after the value gets printed ?

Could you shed some more light on this area which will be appreciable.

Thanks,

Greenhorn
Posts: 26
• Number of slices to send:
Optional 'thank-you' note:
hi! Kumaran..

just change the code from

m = m++; to

m++;

& run your code and 2 will be printed. Hopefully that will clear your confusion.

think of post increment as

m = m++;

as : the original value of m (1) is stored in a temp variable, value of m is incremented & then the temp value is assigned to m(L.H.S).
hence in the next line the value will be 1.

while in pre increment : m = ++m;
the value of m is incemented (to 2), store in a temp variable & then the value of temp is stored in m(L.H.S).

[ July 10, 2007: Message edited by: Mahima Singh ]
[ July 10, 2007: Message edited by: Mahima Singh ]

Keith Lynn
Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by Kumaran Pillai:
Thanks Keith for your brief explanation.

But my doubt is : In the line m = m++ , as you said, the value of M ( which is 1 because of previous increment) will be assigned to M and after it gets incremented as per the Post increment rule. But when i try to print the value of M, instead of giving 2, it is giving the output as 1. Will the value alone be assigned to M and the increment will happen after the value gets printed ?

Could you shed some more light on this area which will be appreciable.

Thanks,

Kumaran,