Link to home
Start Free TrialLog in
Avatar of samsum
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-
Avatar of bobbit31
bobbit31
Flag of United States of America image

not a bug:

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
Avatar of allahabad
allahabad


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.
Whooosh ! Threee answers.
Avatar of samsum

ASKER

no, I know about the x++ and ++x.

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.
Avatar of samsum

ASKER

no, I know about the x++ and ++x.

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



Avatar of samsum

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-
Avatar of Mick Barry
> 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.



   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

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.
Avatar of samsum

ASKER

yes, I tested in C++. X is ONE.
that is why I ask.
luu-
ASKER CERTIFIED SOLUTION
Avatar of ozymandias
ozymandias
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of samsum

ASKER

Thank you all you experts for the clarification.  I encounter this on one of the test.  That is why I got confused.

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
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
and
d) the C++ behaviour is undefined in standard and thus compiler dependant