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?
stupid question OR what.....
Moderator: admin
-
- Site Admin
- Posts: 10066
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: stupid question OR what.....
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.
If you like our products and services, please help us by posting your review here.
Re: stupid question OR what.....
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.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-varint 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.
Anyway, i thank you for your answer.
Who is online
Users browsing this forum: No registered users and 223 guests