• 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

Increment Operator

 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class TrickyTest {

public static void main ( String args [ ] ) {
int i = 10 ; // line 1
i = ++i ; // line 2
i = i++ ; // line 3
System . out . println ( i ); // line 4
}
};

It prints 11. In line 3 , why the increment did not happen?
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PostIncrementOperatorAndAssignment .
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please find the following explaination from this post in javaranh
refer to this post

In a normal situation (like in j = k++) this is what happens:
A.The current value of k is stored temporarily in the stack.
B.The value of k is incremented
C.The value on the stack is assigned to j


This is how it happens in this abnormal situation ( j = j++)
A.The current value of j is stored temporarily in the stack.
B.The value of j is incremented
C.The value on the stack is assigned to j

Hope this would clear your doubt
Regards,
Somesh
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can I say that "i" is assign before the increment operator,so that's why "i" automatic assign the value of before increment.
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Still its not clear.

This prints 4.



This prints 0.

On javaranch faq its mentioned that....



"i++" is evaluated. The value of "i++" is the value of i before the increment happens.
As part of the evaluation of "i++", i is incremented by one. Now i has the value of 1;
The assignment is executed. i is assigned the value of "i++", which is the value of i before the increment - that is, 0.



Now I think its a bug in java in that it behaves differently for i=i++; and i=j++;

On java sun site here Definite Assignment

Its mentioned....

1. V is definitely assigned after ++a, --a, a++, or a-- iff either a is V or V is definitely assigned after the operand expression.

2. V is definitely unassigned after ++a, --a, a++, or a-- iff a is not V and V is definitely unassigned after the operand expression.
3. V is [un]assigned before a iff V is [un]assigned before ++a, --a, a++, or a--.


regards

Naseem
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now I think its a bug in java in that it behaves differently for i=i++; and i=j++;


Do you seriously think that this would be a bug ?

i++ return the value of i before the incrementation.
So after i=i++, i still has the same value, always.
So looping 5 times won't make it change.

k++ returns the value k before the incrementation.
But you're not assigning k to itself.
So after i = k++, i and k both change.

This is not a bug, it's logic
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sivakumar Nachimuthu:
class TrickyTest {

public static void main ( String args [ ] ) {
int i = 10 ; // line 1
i = ++i ; // line 2
i = i++ ; // line 3
System . out . println ( i ); // line 4
}
};

It prints 11. In line 3 , why the increment did not happen?



Java evaluates its expressions left to right. so, in line2, the variable i first incremented then assigned to i.

in line 3, the old value of i will be replaced with produced value from line2, and that happenes before incrementing.
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I got it.
Thanks to all...



i++ is the value of i before increment happens.



First i++ get evaluated which is equivalent to i=i+1; so i becomes 1.
i=value of i before increment happens. // from above quote so i becomes 0.



This is how code works...

1. First i++ get evaluated which is equivalent to i=i+1; so i becomes 1.
j=value of i before increment happens. // from above quote so j becomes 0.

2. Second time. i++ get evaluated which is equivalent to i=i+1; so i becomes 2.
j=value of i before increment happens. // from above quote so j becomes 1.

3. Third time. i++ get evaluated which is equivalent to i=i+1; so i
becomes 3.
j=value of i before increment happens. // from above quote so j becomes 2.

4. Fourth time. i++ get evaluated which is equivalent to i=i+1; so i
becomes 4 now.
j=value of i before increment happens. // from above quote so j becomes 3.

Thanks & Regards


Naseem Khan
[ May 18, 2006: Message edited by: Naseem Khan ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic