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

14

140

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

Candace answered 3/3, 2010 at 12:21 Comment(8)
Always avoid ambiguous statements :)Arielariela
@Prasoon Saurav Unlike C and C++, Java and C# have strictly defined order of evaluation, so these statements are not ambiguous.Winterfeed
I know that but still those statements are not(can not be) used for practical purpose so one must avoid it.Arielariela
https://mcmap.net/q/103396/-java-increment-decrement-operators-how-they-behave-what-39-s-the-functionalityOech
@PeteKirkham It's over six years later, but I still want to point out that "ambiguous", in this situation, is ambiguous -- it could mean "the compiler doesn't know what to put", or it could mean "The programmer has no idea what it means".Hendry
@QPaysTaxes Java programmers should know what pre and post-increment mean and that statements are evaluated left to right, so if the programmer really has no idea what it means then they aren't a Java programmer. Which isn't to say it's a clear way of expressing the semantics, but it not in any way ambiguous.Winterfeed
i=++a + ++a + a++; => i=7 + 8 + 5; (a=8) since post increment has highest precedence, which means a++ should executed first.Tampa
Understand that Ankit is trying to contrast the two ways of increment in his example code; just in case someone is wondering like me: putting the example code exactly as written in Eclipse gives 38 and 29.Orling
S
175

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)

The main point is that ++a increments the value and immediately returns it.

a++ also increments the value (in the background) but returns unchanged value of the variable - what looks like it is executed later.

Subjection answered 3/3, 2010 at 12:25 Comment(5)
Are you sure a == 9 in the second one?Winterfeed
i=++a + ++a + a++; => i=7 + 8 + 5; (a=8) since post increment has highest precedence, does a++ is executed first?Tampa
complicated example to something that is easy to explain.Solace
Is this answer is the same for c# and c++?Dielectric
@Dennis b = a++ results in b = 1, a = 2, then c = ++b results in c = 2, b = 2Sommelier
A
270

++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

codaddict explains your particular snippet.

Ardene answered 3/3, 2010 at 12:27 Comment(1)
Instead of "uses", I think a less confusing word would have been "evaluates".Sallyanne
S
175

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)

The main point is that ++a increments the value and immediately returns it.

a++ also increments the value (in the background) but returns unchanged value of the variable - what looks like it is executed later.

Subjection answered 3/3, 2010 at 12:25 Comment(5)
Are you sure a == 9 in the second one?Winterfeed
i=++a + ++a + a++; => i=7 + 8 + 5; (a=8) since post increment has highest precedence, does a++ is executed first?Tampa
complicated example to something that is easy to explain.Solace
Is this answer is the same for c# and c++?Dielectric
@Dennis b = a++ results in b = 1, a = 2, then c = ++b results in c = 2, b = 2Sommelier
B
81

In both cases it first calculates value, but in post-increment it holds old value and after calculating returns it

++a

  1. a = a + 1;
  2. return a;

a++

  1. temp = a;
  2. a = a + 1;
  3. return temp;
Blevins answered 19/9, 2017 at 6:13 Comment(0)
C
26
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.

Crowberry answered 3/3, 2010 at 12:25 Comment(1)
these statements work right to left or left to right?Reed
S
10

++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.

Swats answered 3/3, 2010 at 12:24 Comment(1)
@KlasLindbäck commutative means that you can swap both expressions and still get the same result. So a++ + ++a == ++a + a++ (5 + 7 == 6 + 6; a == 7 at the end).Swats
H
8

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
Hispanicism answered 5/9, 2014 at 7:57 Comment(1)
a is 39 in c++. Why so?Penalize
H
6

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
Hedgehog answered 15/9, 2016 at 17:46 Comment(1)
yeah, this is the only right answer corresponding to the crappy code provided. Other ppl have interpreted the code to make it a legitimate valid question.On
D
6

++a is prefix increment operator:

  • the result is calculated and stored first,
  • then the variable is used.

a++ is postfix increment operator:

  • the variable is used first,
  • then the result is calculated and stored.

Once you remember the rules, EZ for ya to calculate everything!

Dipietro answered 29/6, 2017 at 16:2 Comment(0)
M
4

Presuming that you meant

int a=5; int i;

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

System.out.println(i);

a=5;

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

System.out.println(i);

a=5;

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

System.out.println(a);

This evaluates to:

i = (6, a is now 6) + (7, a is now 7) + (7, a is now 8)

so i is 6 + 7 + 7 = 20 and so 20 is printed.

i = (5, a is now 6) + (7, a is now 7) + (8, a is now 8)

so i is 5 + 7 + 8 = 20 and so 20 is printed again.

a = (6, a is now 6) + (7, a is now 7) + (7, a is now 8)

and after all of the right hand side is evaluated (including setting a to 8) THEN a is set to 6 + 7 + 7 = 20 and so 20 is printed a final time.

Millpond answered 14/9, 2017 at 10:5 Comment(0)
B
3

when a is 5, then a++ gives a 5 to the expression and increments a afterwards, while ++a increments a before passing the number to the expression (which gives a 6 to the expression in this case).

So you calculate

i = 6 + 7 + 7
i = 5 + 7 + 8
Billmyre answered 3/3, 2010 at 12:26 Comment(0)
S
3

Pre-increment means that the variable is incremented BEFORE it's evaluated in the expression. Post-increment means that the variable is incremented AFTER it has been evaluated for use in the expression.

Therefore, look carefully and you'll see that all three assignments are arithmetically equivalent.

Stature answered 4/11, 2018 at 19:3 Comment(0)
E
2

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
Everywhere answered 12/6, 2016 at 20:16 Comment(0)
L
1

I believe you are executing all these statements differently
executing together will result => 38 ,29

int a=5,i;
i=++a + ++a + a++;
//this means i= 6+7+7=20 and when this result is stored in i,
//then last *a* will be incremented <br>
i=a++ + ++a + ++a;
//this means i= 5+7+8=20 (this could be complicated, 
//but its working like this),<br>
a=++a + ++a + a++;
//as a is 6+7+7=20 (this is incremented like this)
Lysimachus answered 23/10, 2017 at 23:1 Comment(0)
C
0
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.

Coopt answered 30/10, 2015 at 5:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.