Page 1 of 1

stupid question OR what.....

Posted: Wed Mar 06, 2013 5:53 pm
by Guest
I think i'm studying way too much that i can't figure why this happening:

assume the following code is inside main method.

int i = 0;
int m =3;

System.out.println( i = m++ );
System.out.println( i + " : " + m );

3
3 : 4

QUESTION: Why value of ( i ) never changed?

Re: stupid question OR what.....

Posted: Wed Mar 06, 2013 6:46 pm
by admin
Not a stupid question. You need to read about how pre-increment and post-increment works. The following link has good explanation: http://stackoverflow.com/questions/6175 ... ar-and-var
int a = 5, b;

post increment : b = a++; : a is first transferred to b and then a is incremented, so now b is 5, and a is 6 The effect is b = a; a = a + 1;

pre increment: b = ++a; : first a is incremented and then the result is transferred into b, so now a is 7 and also b is 7. The effect is a = a + 1; b = a

a++ and ++a staying independently act in the similar way. In the loop examples you have presented, the increment operators is not associated in any expression, and are independent. Therefore these two in this particular implementation is identical.

Re: stupid question OR what.....

Posted: Wed Mar 06, 2013 8:08 pm
by Guest
admin wrote:Not a stupid question. You need to read about how pre-increment and post-increment works. The following link has good explanation: http://stackoverflow.com/questions/6175 ... ar-and-var
int a = 5, b;

post increment : b = a++; : a is first transferred to b and then a is incremented, so now b is 5, and a is 6 The effect is b = a; a = a + 1;

pre increment: b = ++a; : first a is incremented and then the result is transferred into b, so now a is 7 and also b is 7. The effect is a = a + 1; b = a

a++ and ++a staying independently act in the similar way. In the loop examples you have presented, the increment operators is not associated in any expression, and are independent. Therefore these two in this particular implementation is identical.
after i posted and before i read your answer i was leaving the library heading home, while driving i thought about the question again.... i figured out the answer while driving home!!... i guess my brain was protesting the long study hours and turned foggy on me.

Anyway, i thank you for your answer.