0

I have a simple question that I cannot find the answer to in any java videos or forums. If someone can please explain it to me, why my simple post increment is not working. I start with a value of 2 and never see it increment after 1 or 30 lines.

MY CODE

int x=2;
        x=x++;
        System.out.println("value of x "+x);  
        System.out.println("value of x "+x);   
        System.out.println("value of x "+x);

all the answers are 2. What happened to my increment? x never got 1 added to it post the assigning. this works if I use 2 different variables and x goes up but I need to see it with one/same variable

HKM
  • 1
  • 3
  • 1
    ++ increments x and returns its old value – sleepToken Jan 23 '20 at 16:21
  • 5
    Does this answer your question? [What is x after "x = x++"?](https://stackoverflow.com/questions/7911776/what-is-x-after-x-x) – sleepToken Jan 23 '20 at 16:21
  • The instruction for an increment is just x++; It won't work if put x = beforehand – kumesana Jan 23 '20 at 16:21
  • I still don't understand why x never got incremented or held its incremented value through out the code. x=x++; after assigning old value then adding one to it by java operator definition should carry the new value. I did look at the whole temp = x and so on but makes no sense why that dead end circle would happened. I thought x++ means it would add 1 after assignment regardless. – HKM Jan 23 '20 at 18:20
  • @HKM What did you try to achieve while doing `x=x++` ? – Nicolas Jan 23 '20 at 18:20
  • Well, I wanted to see real cause of this fail in Java because everyone is saying post increment will first assign old value then after one use of the variable it will be incremented. It's not true. The value never gets any increment. The whole idea of a temp = x, then x gets incremented and then x = temp makes no sense. Its a good way to throw the topic under the carpet but java developers would do that is beyond me. There has to be reason why x does not hold its incremented value. given x is 1 x=x++; 1 is assigned to x and after completing the statements x is now 2, why go back to 1. – HKM Jan 23 '20 at 19:59
  • the increment is not made at a random time. It is made at the moment its expression is evaluated. So, in x = x++; first you evaluate x++ which increments x. And after all that is done, you assign the resulting value to x. The resulting value is the value before the incrementation, so it is 1, therefore x goes back to 1. You seem to expect that the incrementation would be delayed for at least after the entire instruction is executed, which is rather weird. What would be the point of putting things in some order rather than another order in that case? – kumesana Jan 24 '20 at 00:59
  • It is also weird that you would raise an issue here. Why would you *want* to check what x = x++; does when everyone tells you that such a line is stupid and you shouldn't have it at all? – kumesana Jan 24 '20 at 01:00

1 Answers1

1

Remove the x=x++;:

int x=2;
    x++;
    System.out.println("value of x "+x);  
    System.out.println("value of x "+x);   
    System.out.println("value of x "+x);
Stefano Maglione
  • 3,946
  • 11
  • 49
  • 96