Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 311

# 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-
0
samsum
• 5
• 5
• 4
• +3
1 Solution

Commented:
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
0

Commented:

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

Commented:
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.
0

Commented:
0

Author Commented:
no, I know about the x++ and ++x.

Let analyse  x=x++;

x=x;
x++;

x should be ONE.

luu-

0

Commented:
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.
0

Author Commented:
no, I know about the x++ and ++x.

Let analyse  x=x++;

x=x;
x++;

x should be ONE.

luu-

0

Commented:
No. That is wrong.

x = 0;
x = x;
x++;

is not the same as :

x = 0;
x = x++;

0

Author Commented:
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-
0

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

0

Commented:
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

0

Commented:
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.
0

Author Commented:
yes, I tested in C++. X is ONE.
luu-
0

Commented:
There is no doubt that these two probrams produce different results :

#include <iostream.h>

int main(){
int x = 0;
x = x++;
cout << "x = " << x;
return 0;
}

public class Test{
public static void main(String[] args){
int x = 0;
x = x++;
System.out.println(x);
}
}

However, I would suggest it is a difference in the way they handle precedence rather than a bug in java.
0

Commented:
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.
0

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

luu-
0

Commented:
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+++?
0

Commented:
This is the discussion on the other side of the fence if interested:

http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_20558014.html
0

Commented:
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
0

Commented:
and
d) the C++ behaviour is undefined in standard and thus compiler dependant
0

## Featured Post

• 5
• 5
• 4
• +3
Tackle projects and never again get stuck behind a technical roadblock.