stupid question OR what.....

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Guest

stupid question OR what.....

Post 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?

admin
Site Admin
Posts: 10066
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: stupid question OR what.....

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

Re: stupid question OR what.....

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 223 guests