Post Increment Of Variable | What Is x After “x = x++”? In Java

Here in this Tutorial, we will learn increment and, We will come to know What is x after “x = x++”? in Java where Post Increment of Variable is used where the value of x will increment first and then it will be stored in x before increasing as per post increment rule that first, it will be used and then increment will occur.

What Is Post Increment Of Variable?

As here given as x=x++ which X++ and then store it in x here X++ means post increment which numerically means x = x+1 but with a condition so we will discuss the condition of post increment.

Post increment has property as it will first use the variable with the previous value and then it will update the value.

Let’s understand with an example

Example

Let’s take the example of a given expression to know better. as the expression shows the first thing to do in increment but as it is a post increment so first, it will use this previous value then it  will update

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

Here the output of the above code will be as 9

OUTPUT

Post Increment Of Variable

As we can see the output is 9 as before the increment previous value was stored to the x and that needed to be printed so it simply printed the previous value.

Exception

There can be a change in the value of x that means the answer could be 10 only if there could be a pre- increment instant of post increment if the expression could be x = ++x then here it says first the value of x needs to increment and then store in x so the matter would have been changed.

Learn More about Posts and pre Increments at Post Increment

And also visit JAVA TUTORIALFor knowing more about other problems faced in java along with solutions.

Leave a Comment

%d bloggers like this: