# How do the post increment (i++) and pre increment (++i) operators work in Java?

asked Mar 3, 2010

Can you explain to me the output of this Java code?

``````int a=5,i;

i=++a + ++a + a++;
i=a++ + ++a + ++a;
a=++a + ++a + a++;

System.out.println(a);
System.out.println(i);
``````

The output is 20 in both cases

answered Jan 3, 2010 by

`++a` increments `a` before it is evaluated. `a++` evaluates `a` and then increments it.

Related to your expression given:

``````i = ((++a) + (++a) + (a++)) == ((6) + (7) + (7)); // a is 8 at the end
i = ((a++) + (++a) + (++a)) == ((5) + (7) + (8)); // a is 8 at the end
``````

The parenteses I used above are implicitly used by Java. If you look at the terms this way you can easily see, that they are both the same as they are commutative.

answered Jan 3, 2010
``````i=++a + ++a + a++;
``````

is

``````i = 6 + 7 + 7
``````

Working: increment a to 6 (current value 6) + increment a to 7 (current value 7). Sum is 13 now add it to current value of a (=7) and then increment a to 8. Sum is 20 and value of a after the assignment completes is 8.

``````i=a++ + ++a + ++a;
``````

is

``````i=5 + 7 + 8
``````

Working: At the start value of a is 5. Use it in the addition and then increment it to 6 (current value 6). Increment a from current value 6 to 7 to get other operand of +. Sum is 12 and current value of a is 7. Next increment a from 7 to 8 (current value = 8) and add it to previous sum 12 to get 20.

answered Mar 3, 2010

Does this help?

``````a = 5;
i=++a + ++a + a++; =>
i=6 + 7 + 7; (a=8)

a = 5;
i=a++ + ++a + ++a; =>
i=5 + 7 + 8; (a=8)
``````
answered Mar 3, 2010 by

`++a` increments and then uses the variable.
`a++` uses and then increments the variable.

If you have

``````a = 1;
``````

and you do

``````System.out.println(a++); //You will see 1

//Now a is 2

System.out.println(++a); //You will see 3
``````

answered Jan 5, 2014 by

In the above example

``````int a = 5,i;

i=++a + ++a + a++;        //Ans: i = 6 + 7 + 7 = 20 then a = 8

i=a++ + ++a + ++a;        //Ans: i = 8 + 10 + 11 = 29 then a = 11

a=++a + ++a + a++;        //Ans: a = 12 + 13 + 13 = 38

System.out.println(a);    //Ans: a = 38

System.out.println(i);    //Ans: i = 29
``````
answered Jan 30, 2015
``````a=5; i=++a + ++a + a++;
``````

is

``````i = 7 + 6 + 7
``````

Working: pre/post increment has "right to left" Associativity , and pre has precedence over post , so first of all pre increment will be solve as `(++a + ++a) => 7 + 6` . then `a=7` is provided to post increment => `7 + 6 + 7 =20` and `a =8`.

``````a=5; i=a++ + ++a + ++a;
``````

is

``````i=7 + 7 + 6
``````

Working: pre/post increment has "right to left" Associativity , and pre has precedence over post , so first of all pre increment will be solve as `(++a + ++a) => 7 + 6`.then `a=7` is provided to post increment => `7 + 7 + 6 =20` and `a =8`.

answered Jan 12, 2016

pre-increment and post increment are equivalent if not in an expression

``````int j =0;
int r=0
for(int v = 0; v<10; ++v) {
++r;
j++;
System.out.println(j+" "+r);
}
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
``````
answered Jan 15, 2016

I believe however if you combine all of your statements and run it in Java 8.1 you will get a different answer, at least that's what my experience says.

The code will work like this:

``````int a=5,i;

i=++a + ++a + a++;            /*a = 5;
i=++a + ++a + a++; =>
i=6 + 7 + 7; (a=8); i=20;*/

i=a++ + ++a + ++a;           /*a = 5;
i=a++ + ++a + ++a; =>
i=8 + 10 + 11; (a=11); i=29;*/

a=++a + ++a + a++;            /*a=5;
a=++a + ++a + a++; =>
a=12 + 13 + 13;  a=38;*/

System.out.println(a);        //output: 38
System.out.println(i);         //output: 29
``````
answered Sep 15, 2017 by

Just refer these principles and that image and you would be pro in a matter of minutes.

Principle:-

1.) When the unary operators ++ and -- are used in prefix notation, the value of the variable increments/decrements just before the variable is used in an expression.

2.) When the unary operators ++ and -- are used in postfix notation, the value of the variable increments/decrements just after the variable is used in an expression.

Example:- The evaluation of the expression in image starts from left to right. Assume a to be 10.

``````Your Question:-

int a = 5, i;

i = ++a + ++a + a++;
i = a++ + ++a + ++a;
a = ++a + ++a + a++;

System.out.println(a);
System.out.println(i);

i = 6 + 7 + 7; [a = 8]
i = 8 + 10 + 11;
a = 12 + 13 + 13; [a = 14]

O/P :- 38
29
``````