What will be printed out in the following code and what is the difference between this and your code? That is the question Mr Navarov should answer instead of being insolent.
int n = 1;
int y = n++;
System.out.println( "n =" + n );
> I am sure that if you give 10 programmers this problem and ask them what the output will be, more than 5 would probably say 2.
Oh yes -"programmers" will probably say 1 or 2. But programmers will say "Such operations like a = a++ is ambiguous and should never be used. Result of such operatons is unpredictible and depends on compiler/interpreter realisation".
> What will be printed out in the following code and what is the difference between this and your code?
> int y = n++;
This is simple and straight code for variable "n": increase n by one. Try to explain in same way step-by-step changes of variable "a" within your first piece of code.
> Oh yes -"programmers" will probably say 1 or 2. But
> programmers will say "Such operations like a = a++ is
> ambiguous and should never be used. Result of such
> operatons is unpredictible and depends on
> compiler/interpreter realisation".
To Mr Nazarov : so that you don't know why the value it will print, why you said that
"Read mannual about prefix and postfix ?".
Although this operation is ambiguous and should never be used, I only want to know why the result is 1 ? Thanks alot
> so that you don't know why the value it will print, why you said that
"Read mannual about prefix and postfix ?".
This is first part of selfeducation you should perform.
> I only want to know why the result is 1
Because your expression virtually equals to followed set:
a = a + 1; // ++
a = 1; // = with original a value before ++
Read ++ manual and good programming book, I insist...
Thanks because of your answer.
> Because your expression virtually equals to followed
> set:
> a = a + 1; // ++
> a = 1; // = with original a value before ++
> Read ++ manual and good programming book, I insist...
Every body knowed
n = a++ ;
will set
1. n = a;
2. a = a + 1; -> so a will be 2
So why
a = a++; don't set
1. a = a;
2. a = a + 1;
May be i'm wrong ?
> Every body knowed
> n = a++ ;
> will set
> 1. n = a;
> 2. a = a + 1; -> so a will be 2
>
> So why
> a = a++; don't set
> 1. a = a;
> 2. a = a + 1;
>
> May be i'm wrong ?
Yes you are wrong. In Java, the right side of an = is evaluated first, as BIH001 says. To use your examples.
n = a++
1. expression = a
2. a = a + 1
3. n = expression
a = a++
1. expression = a
2. a = a + 1
3. a = expression
A Java implementation that supports the Java Language Specification must work this way.