✍️ Understanding the difference between Pre-Increment and Post-Increment Operator in Java (and C and C++)

In this article, I will show you a straight-forward way of looking at pre and post increment operators that are common in languages like C, C++ and Java

✍️ Understanding the difference between Pre-Increment and Post-Increment Operator in Java (and C and C++)
Photo by Max Duzij / Unsplash

Hi! My name is Narendra Vardi, and I write about my learnings, observations and trends in the Software world. Besides software, I also talk about photography, travel stories, books and movies. If that's something that interests you, consider subscribing.


💡
There is a bit of easy code in this article. Don't feel intimidated! :D

Before we get into the details of the article, let me ask you a question.

Can you tell me what will be the output of following program?

class Main {
    public static void main(String[] args) {
        int a = 2;
        int b = 2;
        a++;
        b++;
        System.out.println(a);
        System.out.println(b);
    }
}

Example #1

It's fairly easy one right?

The output must be the following, correct?

3
3

Output of Example #1


Now, let's take one more sample code. What will be the output of following program?

class Main {
    public static void main(String[] args) {
        int a = 2;
        int b = 2;

        System.out.println(++a);
        System.out.println(b++);
    }
}

Example #2

It's fairly easy but also a tricky one isn't it?

So, the output is:

3
2

Output of Example #2

Now if you understood this output, you can stop reading this article! (Having said that you can share some feedback on how the article is provided that you know the concepts 😄)

If you didn't understand this output, let me tell you what's happening.

When we are taught these operators in colleges or beginner books, the first example they would give is using these operators as a single statement.

a++; // single statement
++a; // single statement

Because of the way it is used, we only see ++ as a increment operator and we don't really focus on whats the difference between ++a and a++

Now when we combine ++a and a++ with other operators (or used in methods as Example #2), we will get to know the difference.

So, what's the major difference?

Before we get into the differences, lets see what's the increment operator doing.

What's the increment operator?

Increment operator has two functionalities.

  1. It increments the value of the variable and
  2. It returns a value (What value - we will talk about it)

Now talking about It increments the value of the variable functionality, this is same for ++a and a++ . a value will be incremented by 1 in both the usages.

The major difference is observed with the second functionality, It returns a value

  • ++a -> returns incremented value where as
  • a++ -> returns the value and does the increment later.

And that's the reason why System.out.println(++a); returned 3 when a was initially 2 but System.out.println(a++); remained 2.

To clarify even better, I wrote a Java class that helps us visualise the work of Pre and Post Increment Operators.

Here's the code for the same. Take a look at it and let me know what you think!

public class PostIncrement {
  private int x;

  public PostIncrement(int x) {
    this.x = x;
  }

  // similar to x++;
  public int increment() {
    int temp = x;
    x = x + 1;
    return temp;
  }
  
  public int getX() {
    return x;
  }
  // add a toString() method.
}

PostIncrement.java

public class PreIncrement {
    private int x;

    public PreIncrement(int x) {
        this.x = x;
    }

    // similar to ++x
    public int increment() {
        x = x + 1;
        return x;
    }

    public int getX() {
        return x;
    }
    // add a toString() method.
}

PreIncrement.java

Finally, let's see how the example #2 looks using the increment class that I have written.

class Main {
    public static void main(String[] args) {
        PreIncrement a = new PreIncrement(2);
        PostIncrement b = new PostIncrement(2);
        System.out.println(a.increment());
        System.out.println(b.increment());
    }
}

Example #2 using PostIncrement and PreIncrement classes.

With the provided examples, were you able to identify clearly understand what's the difference between Pre and Post Increment Operators?

Can you write the classes for Decrement operators as well? Please feel free to comment below or shoot an email.


Share this article

Copy and share this article: narendravardi.com/pre-post-increment/


Recommendations

If you liked this article, you might also enjoy reading the following.


❤️ Enjoyed this article?

Forward to a friend and tell them where they can subscribe (hint: it's here).

Anything else? Comment below to say hello, or drop an email!