-5

After running this program, I am getting value of 'a' as 5 instead of 6.Why?

public class Test
{
    public static void main(String args[])
    {
      int a=5;
      a=a++;                        //post increment operator
      System. out. println (a);  //Output
    }
}

Why is this the output of the program?

StuartM
  • 6,743
  • 18
  • 84
  • 160
JavaSat
  • 34
  • 2
  • 8
  • 3
    You're answering your title question in your first sentence. The answer can be found on this site btw. It's because it's saving the old value, incrementing, then returning the old value, which you assign. – keyser Mar 31 '15 at 14:33
  • What do you think the answer should be? – Andy Turner Mar 31 '15 at 14:37
  • FYI you really only needed to google `java post increment operator` to find numerous duplicates on StackOverflow in addition to various other sites with detailed explanations about how the post increment operator works. – tnw Mar 31 '15 at 14:41
  • `What is the output of the program?` 5 – CodeCamper Mar 31 '15 at 14:45
  • @CodeCamper The output is 5 instead of 6. Why? – JavaSat Mar 31 '15 at 15:32
  • @tnw My question is different from possible duplicate. – JavaSat Mar 31 '15 at 15:33
  • @AndyTurner The answer should be 6.But the output coming is 5.Why? – JavaSat Mar 31 '15 at 15:33
  • What do you think the value of `b` would be if you wrote `b = a++`; hopefully 5. Why should it be any different if `b` is replaced with `a`? – Andy Turner Mar 31 '15 at 15:49
  • @JavaSat check my answer below and tell me if that makes sense to you. – CodeCamper Mar 31 '15 at 16:10
  • @CodeCamper Thanks for clarifying my doubt. Please tell me how to take garbage 'a' variable?It might be important for me. – JavaSat Mar 31 '15 at 17:39
  • @AndyTurner Yes value of b will be 5 but at the same time the value of a will get incremented and value of a will be 6. – JavaSat Mar 31 '15 at 17:40

2 Answers2

0

That is because of the post increment. In post increments, the value before the increment is returned and then the increment is performed. This line would achieve the same result System.out.println(a++);. The value of a before the increment should be printed.

tnw
  • 13,521
  • 15
  • 70
  • 111
Christopher Z
  • 899
  • 1
  • 12
  • 32
  • But here my question is bit different. After post increment the value should become incremented i.e. a=5; a++; On printing a,the output would be 6. But if we use a=a++; instead of a++; and then print then the incremented value i.e. 6 would not get printed instead the output would be 5. Why? Please explain. – JavaSat Mar 31 '15 at 15:26
  • As explained in the duplicate and here as well, the value IS getting incrememted but you're setting it back to the original value which is returned by the expression `a++`. If you want it to return the value *after* incrememting, you need to use `++a` instead. Read [this answer](http://stackoverflow.com/a/5413593/770270) *carefully*. This is a very subtle detail but is fairly straight forward. – tnw Mar 31 '15 at 15:39
0

Scenario 1 (a finally equals 5)

a=5;
a=a++;

IS NOT THE SAME THING AS

Scenario 2 (a finally equals 6)

a=5;
int a

To understand this you must break down what is happening in Scenario 2.

  1. Create a new primitive int equal to 5 and place a reference to it inside a.
  2. Create a new primitive int equal to the value a is referring to plus 1.
  3. Store the reference to that new primitive int inside a.

Scenario 1 is totally different.

  1. Create a new primitive int equal to 5 and place a reference to it inside a.
  2. Create a new primitive int equal to the value a is referring to.
  3. Add 1 to the original primitive int which has probably already been handed off to the garbage collector.

The reason you are confused is because you think a is always referring to the same place in memory. As soon as you said a=a you changed locations in memory. This is now a NEW a, not the same old a, so essentially your ++ is doing nothing in this case.

a=a++; doesn't mean let a stay as a and then after this statement add 1. What it actually means is make a equal to a NEW int that just so happens to have the same value as a and then add 1 to that old int I just threw into the black hole.

Edit: response to comments

If you do not want to lose your original a variable you will have to write your code differently.

int a = 5;
int b = a; //stores the original value
a++; //now a equals 6
CodeCamper
  • 6,609
  • 6
  • 44
  • 94