samsum
asked on
is this a Java bug x= x++
in Java ...
*******************
int x=0;
x = x++;
System.out.println(x);
*******************
The result is ZERO.
------------------------
in C++
The result is ONE.
What do you think?
thank.
luu-
*******************
int x=0;
x = x++;
System.out.println(x);
*******************
The result is ZERO.
------------------------
in C++
The result is ONE.
What do you think?
thank.
luu-
int x=0;
x = x++;
System.out.println(x); you will get 0, becuase this is post increment.
but :
x=++x ;
System.out.println(1); you will get 1, becuase this is pre increment.
And samething happens in 'c' too.
No. It is by design.
x = x++;
means that x is assigned the value of x and then x is incremented AFTERWARDS.
x = ++x;
would produce the answer 1 because x is incremented first and then assigned the value of x.
x = x++;
means that x is assigned the value of x and then x is incremented AFTERWARDS.
x = ++x;
would produce the answer 1 because x is incremented first and then assigned the value of x.
Whooosh ! Threee answers.
ASKER
no, I know about the x++ and ++x.
Let analyse x=x++;
x=x;
x++;
x should be ONE.
luu-
Let analyse x=x++;
x=x;
x++;
x should be ONE.
luu-
Yes this will be 1.
x=x++; // x is 0
x=x; // x is 0
x++; // x has become 1, this not an assignment
but if you write
x=x++; in place of x++ in the last line, you will get x=0.
x=x++; // x is 0
x=x; // x is 0
x++; // x has become 1, this not an assignment
but if you write
x=x++; in place of x++ in the last line, you will get x=0.
ASKER
no, I know about the x++ and ++x.
Let analyse x=x++;
x=x;
x++;
x should be ONE.
luu-
Let analyse x=x++;
x=x;
x++;
x should be ONE.
luu-
No. That is wrong.
x = 0;
x = x;
x++;
is not the same as :
x = 0;
x = x++;
x = 0;
x = x;
x++;
is not the same as :
x = 0;
x = x++;
ASKER
ok, then, what is the process of x= x++;
x = x++;
"means that x is assigned the value of x and then x is incremented AFTERWARDS."
AFTERWARDS, shouldn't it be ONE?
luu-
x = x++;
"means that x is assigned the value of x and then x is incremented AFTERWARDS."
AFTERWARDS, shouldn't it be ONE?
luu-
> in C++
> The result is ONE.
Have you tested that?
It's been a while since I wrote any C++ but from memory it would also be zero.
> The result is ONE.
Have you tested that?
It's been a while since I wrote any C++ but from memory it would also be zero.
int x = 0;
x = x++; // result: 0, x is not incremented
original value of x is saved (x0rig)
x is incremented
x0rig is assigned to x
therefore, x will always equal original value
x = x++; // result: 0, x is not incremented
original value of x is saved (x0rig)
x is incremented
x0rig is assigned to x
therefore, x will always equal original value
The unary++ takes precedence over the = operator.
The unary++ increments its operand but returns the previous
value of the operand for the evaluation of the expression.
The unary++ increments its operand but returns the previous
value of the operand for the evaluation of the expression.
ASKER
yes, I tested in C++. X is ONE.
that is why I ask.
luu-
that is why I ask.
luu-
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Well zero is certainly the correct result using Java.
According to C/C++ operator precedence I would expect the result to also be zero. I don't have a C++ compiler handy at the moment, but will test it out when I do.
According to C/C++ operator precedence I would expect the result to also be zero. I don't have a C++ compiler handy at the moment, but will test it out when I do.
ASKER
Thank you all you experts for the clarification. I encounter this on one of the test. That is why I got confused.
luu-
luu-
everyone, just because the C++ compiler gives an answer (1) for the code DOES NOT justify the behavior of this piece of code to be a standard one (unless the ANSI C++ rules get changed). Java handles some stuff differently from C++. Ever wonder why it's called Java and not C+++?
This is the discussion on the other side of the fence if interested:
https://www.experts-exchange.com/questions/20558014/what-is-x-x.html
https://www.experts-exchange.com/questions/20558014/what-is-x-x.html
n_fortynine : Yes. I think that was the point we were making.
a) it is not a bug
b) it is normal and standard for java to do this
c) C++ and Java do things differently
a) it is not a bug
b) it is normal and standard for java to do this
c) C++ and Java do things differently
and
d) the C++ behaviour is undefined in standard and thus compiler dependant
d) the C++ behaviour is undefined in standard and thus compiler dependant
x = x++; // this says x = 0 and after you do the assignment increment x by 1
x = ++x; // this says increment x by 1 and then assign it to x