i + + and + + i in Java

Posted by richie on Wed, 29 Dec 2021 16:11:23 +0100

i++

Many times, we understand i + + as using first and then increasing by 1. According to this understanding, we find that sometimes it can work, but sometimes it can't explain some problems, so this understanding is problematic.

i + + this expression does two things: (1) realize the self increment 1 of i, that is, i = i+1 (2) the whole expression returns the old value before i is not self increment 1.

Therefore, we can disassemble i + + into the following three steps:

int original_i = i;  //(1) Keep the old value of i first
i = i + 1;			//(2) Self increase of i
return original_i;  //(3) Return the value of the expression i + +, that is, the old value of I, original_i

The following is a practical example to illustrate that these three steps are equivalent to i + +.

Example 1

public class Test1{

	public static void main(String[] args) {
		
		int i = 0, b = 0;
		
		b = i++;
		
		System.out.println("i The value of is" + i);
		
		System.out.println("b The value of is" + b);
	}

}

In example 1, we directly assign i + + to b, and the final printed result is:

i The value of is 1
b The value of is 0

Next, change the code and replace it with the previous three steps:

public class Test1{

	public static void main(String[] args) {
		
		int i = 0, b = 0;
		
		//Replace the original "b = i + +;" Expression, replace with the following three expressions
		int original_i = i;  
		i = i + 1;			
		b = original_i;
		
		System.out.println("i The value of is" + i);
		
		System.out.println("b The value of is" + b);
	}

}

The result is the same

i The value of is 1
b The value of is 0

It shows that this understanding is feasible.

We found that it is also feasible to use first and then add 1. In the case of example 1, it is really no problem.

Let's give another example.

Example 2

public class Test2{

	public static void main(String[] args) {
		
		int a = 0;
		
		for (int i = 0; i < 10; i++ ) {
			a = a++;
		}
		
		System.out.println(a);
	}
}

Let's start with the answer. The result of example 2 is 0, not 10. Why?

If you understand it by using first and then increasing by 1, you will find that a = a + +; It's such an execution step

public class Test2{

	public static void main(String[] args) {
		
		int a = 0;
		
		for (int i = 0; i < 10; i++ ) {
			//a = a++;
			a = 0;		//First use
			a = a+1;	//Post auto increment 1
		}
		
		System.out.println(a);
	}
}

The final answer is 10, which is obviously wrong.

Instead of using the previous three steps, we get the following a = a + +; Perform the following steps.

public class Test2{

	public static void main(String[] args) {
		
		int a = 0;
		
		for (int i = 0; i < 10; i++ ) {
			//a = a++;
			int original_a = a;	//original_a  = 0
			a = a + 1;			//a self increasing 1
			a = original_a;		//a is replaced by original_a coverage
		}
		
		System.out.println(a);
	}
}

The output is 0, no problem.

In this process, for a = a + +; For this expression, the value of a undergoes a transformation from "0 - > 1 - > 0". First, execute a + + to get that the current value of a is 1, and the return value of the whole a + + expression is the original old value 0 of A. then, assign the return value 0 of the whole a + + expression to a, so the value of a is overwritten again and returns from 1 to 0.

Summary: for an i + + expression executed separately, the value of variable I will increase by 1, while the return value of the entire I + + expression is the value before I does not increase by 1. Compared with the way of using first and then increasing by 1, it is more appropriate to increase by 1 and then return to the old value.

++i

Compared with i + +, the understanding of + + i is not so ambiguous. The same results can be obtained whether it is added by 1 first and then used or disassembled in the following way.

++The expression of i also does two things: (1) realize the self increment 1 of i, that is, i = i+1 (2) the whole expression returns the value of i after self increment 1.

We can disassemble + + i into two steps:

i = i + 1;	//Self increment 1
return i;	//Returns the value of the expression + + i, that is, the value of i increased by 1

Let's take a practical example to verify it.

Example 3

public class Test3{

	public static void main(String[] args) {
		
		int b = 0;
		
		for (int i = 0; i < 10; i++ ) {
			b = ++b;
		}
		
		System.out.println(b);
	}
}

First calculate + + b to get that the value of variable b is 1, and the return value of the whole + + b expression is also 1, and then assign it to b. after the assignment, the value of b is also 1. In this way, after the first round of circulation, the value of b is 1. At the end of the for loop, the value of b is 10.

Summary: for the single execution of + + i expression, the value of variable i increases by 1, and then the value of i after increasing by 1 is returned as the return value of the whole + + i expression.

Through the previous demonstration and discussion, we can finally get such an understanding: whether i + + or + + i, we first increase i by 1, and then return the value of the whole expression, but the former returns the old value of i and the latter returns the latest value of i.

Topics: Java