Help me please

I write this code :int a = 1;a = a++;System.out.println("a = " +a);It printed a= 1; Anybody know why ? Thanks alot.
[157 byte] By [lionkingjavaa] at [2007-10-2 20:03:54]
# 1
Why do you think this result is incorrect or amazing?Read manual about prefix and postfix increments.
Michael.Nazarov@sun.coma at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2
Do not feel insulted by what the previous response says. 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.
Augiea at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3

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 );

Augiea at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

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

Michael.Nazarov@sun.coma at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5

> 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

lionkingjavaa at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

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

Michael.Nazarov@sun.coma at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 7

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 ?

lionkingjavaa at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 8
> Every body knowedWho knows? Show me. Not knows but think they are knows.Why don't it's look like this:1. Operator ++ increase "a" and return previous value (no more "a", just value) to...2. ...operator =. This operator set "n" to 1.
Michael.Nazarov@sun.coma at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 9
a = a++;Means "assign the value of the expression a++ to a". Now the value of the expression a++ is a's value with the side effect of its being bumped. Is it clearer now? This question gets discussed all the time.
BIJ001a at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 10

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

atmguya at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 11
:D Thank you very much. Now i've known why the result is 1.
lionkingjavaa at 2007-7-13 22:43:55 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...