Really Quick n00b question, should take three seconds to answer
Code :
int n = 5;
n = n++;
System.out.println(n);
Well, this prints out 5, can anyone explain please? Thanks!
Re: Really Quick n00b question, should take three seconds to answer
Re: Really Quick n00b question, should take three seconds to answer
Re: Really Quick n00b question, should take three seconds to answer
It is bad coding because n is initialized with 5, then n is incremented to 6 and then n is equal to 6.
When n is incremented to 6 it is the same thing as saying n = n + 1; n++ is called a short hand or idiom.
So what is happening is this...
Code :
int n = 5;
n = (n = n + 1);
System.out.println(n);
!!
Re: Really Quick n00b question, should take three seconds to answer
Quote:
Originally Posted by
yasuocodez
It is bad coding because n is initialized with 5, then n is incremented to 6 and then n is equal to 6.
When n is incremented to 6 it is the same thing as saying n = n + 1; n++ is called a short hand or idiom.
So what is happening is this...
Code :
int n = 5;
n = (n = n + 1);
System.out.println(n);
!!
No; it is bad code, but that's not what is happening, you've missed the whole point - the OP wants to know why the output is 5 instead of 6.
The result of the postfix expression is the original value; the increment operation is performed on the right hand expression value after the assignment and is, in this case, thrown away.