Thomas Sundberg

August 5, 2011

What is the difference between i++ and ++i?

Filed under: Clean Code, Java, Programming, Teaching — Tags: , , , — Thomas Sundberg @ 18:27

This post has been migrated to

During a coaching session I got a question about a loop.

Would there be any difference in the output from a loop defined as

for (int i = 0; i < 3; i++) {
    System.out.print(i + " ");

and a loop defined as

for (int i = 0; i < 3; ++i) {
    System.out.print(i + " ");

in Java?

The answer is (of course) no, the output is identical. Both loops will result in

0 1 2

So what is the difference between i++ and ++i then?

The difference is that

  • ++i increments the value first and then return it
  • i++ return the value first and then increments it

This behavior difference doesn’t matter in a for loop. To see the difference in behaviour, we need to execute something else.

Take a look at

int i = 1;
int j = ++i;

System.out.println("i: " + i + " j: " + j);


int i = 1;
int j = i++;

System.out.println("i: " + i + " j: " + j);

They will produce different output.

The output from the examples above will be:

i: 2 j: 2


i: 2 j: 1

Which is better, ++i or i++?

Which construct should I prefer then? The answer falls back on readability. Understanding that you want to increment a variable with one and then assign it to another variable would mean that you want to execute something like

j = ++i;

which is the equivalent of

i = i + 1;
j = i;

I would prefer the latter example, it is easier for me to read and understand.

If you want to save the current value of a variable and then increment it, you could do:

j = i++;

the equivalent would be

j = i;
i = i + 1;

Here I also think that the latter solution would be the better solution. It is clearer what you mean.

There is no difference when you use ++i or i++ on a line on its own (or in a for loop as above). My normal implementation would be to use i++ and I really don’t have a good reason for doing so. It feels good.

What is a better solution is of course a matter of opinion if the only difference is how it reads. But clean code is about opinions. What is easier to read and understand? To whom is it easier? The answer has to be very easy for anybody to read it. Anybody may be a very junior developer that need to understand he code and change it somehow.



  1. […] 1. stackoverflow: What is the difference between ++i and i++ 2. Thomas Sundberg: What is the difference between i++ and ++i? LD_AddCustomAttr("AdOpt", "1"); LD_AddCustomAttr("Origin", "other"); […]

    Pingback by Increment operator: difference between ++i and i++ « Learntofish's Blog — November 22, 2011 @ 20:39

  2. Fine

    Comment by Birendra — January 18, 2012 @ 05:06

  3. […] Question: Any difference between i++ and ++i?  Answer: When they are used in the loops, they are the same. But when used outside the loops, they behave differently: “i++ return the original value of i and then increase it by 1; ++i increases i by 1 and then returns the new value.” (Source: youtube video: Another explanation in text: […]

    Pingback by Java Tips for Beginners (3): i++,print, loop, format,convert – Data & Web — June 14, 2016 @ 05:27

  4. #include

    int main()
    int I=6;
    printf(“%d %d %d”,++I,++I,++I);
    return 0;
    What is output and explain it clearly

    Comment by Sampath Kumar — August 21, 2016 @ 09:03

    • What happened when you executed it?

      This looks like an assignment for a course or test. You will have to work on it yourself.

      Production code that looks like that is just stupid and if it exists, refactor it.


      Comment by Thomas Sundberg — August 21, 2016 @ 09:08

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at

%d bloggers like this: