0

In the below program

package test;

public class test {
    public static void main(String args[])
    {
        //System.out.println(2+3*4);
        int temp=0;
        temp+=temp++;
        System.out.println(temp);
    }

}

I do not understand why its giving me 0.Can any body please explain it?

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
rocking
  • 4,729
  • 9
  • 30
  • 45

7 Answers7

4

Post increment ++ operator increment the value after the expression.

In the code

temp += temp++; so the expression will be evaluated as

i.e. temp= temp + temp++;

temp = 0 + 0 (value will not increment here as you are using post increment)

temp = 0

Lets take one more example

temp= temp++ + temp; so it will be evaluated as

temp = 0 + 1 (value is incremented here mean after temp++ expression)

then print the value it will show 1.

Kick
  • 4,823
  • 3
  • 22
  • 29
  • Isnt temp+=temp++; similar to temp=temp+temp++ so temp++ will be 1 and now temp=temp+1 that is temp=0+1; so temp will be 1 – rocking Feb 28 '14 at 06:42
  • no dear post increment the value after the expression. Do one thing replace temp++ -> ++temp you will find the answer – Kick Feb 28 '14 at 06:43
  • -1 for the comment "value will not increment as you are using post" for explanation check my answer. – Ravi Kumar Feb 28 '14 at 07:08
  • @RaviKumar the explanation is for temp++ mean there the value will not be incement,it will be incremented after the expression.Also updated answer i thnk it will help u – Kick Feb 28 '14 at 07:33
1

Because if you use variable++ in an expression, the variable will increment after the expression is calculated. If you write:

++variable

The variable will be incremented and then the expression will be calculated.

0

You are assigning the value on the postincrement operator,it will increment the value after the temp is assigned.

Preincrement would be better

++temp
Nambi
  • 11,944
  • 3
  • 37
  • 49
0

++ post increment operation done later, 0+0 = 0 is assigned first to temp and

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
0
temp+=temp++;
--> temp=temp+ temp++ ;
-> temp = 0 + 0 (and now increment temp i.e, after assigning)
-->so, temp = 0
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

I spent some time to find the answer and here what i got

int temp = 0;

temp+=temp++;

ie temp = temp + temp++;

now what happens here step by stem

1 step temp = exp(0 + 0)

2 step temp value will be incremented to 1

3 step temp will be assigned the value 0 from expression 1

so actually in the expression temp value is incremented but again reset to 0 due to assignment operator

here is my code as a proof

int i=0,j=0;
System.out.println(i+=i++);
System.out.println(j+=j++);
System.out.println("i= "+i+", j="+j);
System.out.println(i=i+i++);
System.out.println(j=i+j++);
System.out.println("i= "+i+", j="+j);
j+=i++;
System.out.println("i= "+i+", j="+j);

output is

0
0
i= 0, j=0
0
0
i= 0, j=0
i= 1, j=0

so you can see in the last line value of i is 1 that is it got incremented and the value of j is 0 due to assignment operator

and yes you should use pre increment to make it work as you expected this is just to show why this unexpected behavior.

Ravi Kumar
  • 993
  • 1
  • 12
  • 37
0

Its very simple

temp+=temp++   
temp=temp+temp++;

both the above statement are equivalent.As postfix operator will increment its value after once used.So its 0, and 0+0 makes 0.

Ravi Godara
  • 497
  • 6
  • 20