?
Solved

is this a Java bug x= x++

Posted on 2003-03-20
20
Medium Priority
?
307 Views
Last Modified: 2006-11-17
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
Comment
Question by:samsum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +3
20 Comments
 
LVL 18

Expert Comment

by:bobbit31
ID: 8177270
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
 
LVL 3

Expert Comment

by:allahabad
ID: 8177278

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
 
LVL 15

Expert Comment

by:ozymandias
ID: 8177286
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:ozymandias
ID: 8177291
Whooosh ! Threee answers.
0
 

Author Comment

by:samsum
ID: 8177306
no, I know about the x++ and ++x.

Let analyse  x=x++;

x=x;
x++;

x should be ONE.

luu-


0
 
LVL 3

Expert Comment

by:allahabad
ID: 8177336
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 Comment

by:samsum
ID: 8177340
no, I know about the x++ and ++x.

Let analyse  x=x++;

x=x;
x++;

x should be ONE.

luu-


0
 
LVL 15

Expert Comment

by:ozymandias
ID: 8177371
No. That is wrong.

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

is not the same as :

x = 0;
x = x++;



0
 

Author Comment

by:samsum
ID: 8177408
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
 
LVL 92

Expert Comment

by:objects
ID: 8177450
> 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
 
LVL 18

Expert Comment

by:bobbit31
ID: 8177453
   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
 
LVL 18

Expert Comment

by:bobbit31
ID: 8177463
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 Comment

by:samsum
ID: 8177469
yes, I tested in C++. X is ONE.
that is why I ask.
luu-
0
 
LVL 15

Accepted Solution

by:
ozymandias earned 80 total points
ID: 8177569
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
 
LVL 92

Expert Comment

by:objects
ID: 8177574
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 Comment

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

luu-
0
 
LVL 4

Expert Comment

by:n_fortynine
ID: 8178457
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
 
LVL 92

Expert Comment

by:objects
ID: 8178470
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 8179486
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
 
LVL 92

Expert Comment

by:objects
ID: 8184372
and
d) the C++ behaviour is undefined in standard and thus compiler dependant
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Suggested Courses
Course of the Month14 days, 3 hours left to enroll

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question