English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Front++And Rear++ Detailed Explanation and Example Code of Operations

It is generally believed that the prefix ++ first adds the value of the variable1Then use the increased value1The value after that is used for calculation; the postfix++It is first used in the calculation and then the value is added1.

First, let's look at the first example:

package test;
public class Plus_Test01 {
 public static void main(String[] args) {
  int i = 100;
  i = i++;
  System.out.println(i);
 }
}

Guess what the result is?

Next, let's see the second one:

package test;
public class Plus_Test02 {
 public static void main(String[] args) {
  int k = 100;
  while (true) {
   if (k++ > 100) {
    // System.out.println(k);
    break;
   }
   System.out.println(k);
  }
 }
}

Guess what the result is?

In fact, whether it is prefix++Or postfix++Both are first adding the value of the variable1The real difference between the prefix++After that, the value of the variable is added1After that, use the variable with the increased value for calculation. The real difference between the postfix++First, assign the variable to a temporary variable, and then add the value of the variable.1, and then use that temporary variable for calculation.

For the following code snippet (prefix++) :

int i=1;
int j=++i*5;

The second sentence is actually equivalent to:

i+=1; //Add i1
j=i*5; //Add1Calculate the value with the value after, and this result is:10

For the following code snippet (postfix++) :

int i=1;
int j=i++*5;

The second sentence is equivalent to:

int temp=i;  // Assign i to a temporary variable
i+=1;        //Add i1
j=temp*5;   //calculate with the temporary variable, the result is:5

For the first example, it is equivalent to:

int temp=i;
i+=1;
i=temp; //

So the result should be unchanged, that is100.

The assembly code for the first example is:

 public static void main(java.lang.String[]);
  descriptor: ([Ljava/lang/String;)V
  flags: ACC_PUBLIC, ACC_STATIC
  Code:
  stack=2, locals=2, args_size=1
   0: bipush  100
   2: istore_1
   3: iload_1
   4: iinc   1, 1 //the second local var in the local var plus1
   7: istore_1    //save to the local var
   8: getstatic  #16     // Field java/lang/System.out:Ljava/io/PrintStream;
   11: iload_1 //the loaded parameter is the second one on the stack, that is still100
   12: invokevirtual #22     // Method java/io/PrintStream.println:(I)V
   15: return

For the second example, it is actually not difficult, the result is101,note the flow, do not make such mistakes again in the future. (The flow is: first compare temp=i, temp>100, of course does not hold, i+=1,jump to the syso line, the printed value is of course101,again loop, just like temp=i, temp>100, this time it is true, then i+=1,directly exit the loop without executing the statements inside the while loop).

The assembly code for the second example (only the main method is selected):

 public static void main(java.lang.String[]);
  descriptor: ([Ljava/lang/String;)V
  flags: ACC_PUBLIC, ACC_STATIC
  Code:
  stack=2, locals=2, args_size=1
   0: bipush  100  //100 push
   2: istore_1     //save to the second local var (the first local var is the method parameter)
   3: iload_1     //load from the second local var
   4: iinc   1, 1  //give the local var2increment the int value at position1(increment the local var, the result is still in local var, the int value at position number on the operand stack increases1(unchanged)
   7: bipush  100 //100 push
   9: if_icmple  15 //compare the two int values at the top of the operand stack, if the first is less than or equal to the second, then jump to15line
   12: goto   25 //otherwise jump to25line (i.e., operand stack top1> operand stack top2)
   15: getstatic  #2     // Field java/lang/System.out:Ljava/io/PrintStream;
   18: iload_1 // //load from the first local var
   19: invokevirtual #3     // Method java/io/PrintStream.println:(I)V //call this method
   22: goto   3 //again jump back to3,again loop
   25: return //Exit

The third example:

 package test;
 public class Plus_Test03 {
  static int proPlus() {
   int i = 55;
   int j = ++i;
   return j; //56
  }
  static int postPlus() {
   int i = 55;
   int j = i++;
   return j; //55
  }
  public static void main(String[] args) {
  System.out.println(proPlus());//56
   System.out.println(postPlus());//55
  }
}

Assembly of the third example:

static int proPlus();
 descriptor: ()I
 flags: ACC_STATIC
 Code:
  stack=1, locals=2, args_size=0
   0: bipush  55 //55Push onto the stack
   2: istore_0   //Store the int type at the top of the stack into the first local var
   3: iinc 0, 1 //The first local var adds1
   6: iload_0   //Load from local var
   7: istore_1  //Save to the second local var
   8: iload_1   //The stack top is the second local var
   9: ireturnstatic int postPlus();
 descriptor: ()I
 flags: ACC_STATIC
 Code:
  stack=1, locals=2, args_size=0
   0: bipush  55 
   2: istore_0
   3: iload_0    //Load into the stack
   4: iinc 0, 1 //The first local var adds1
   7: istore_1
   8: iload_1
   9: ireturn

As can be seen, prefix++ And Rear++The difference is the blue parentheses above (//The first local var adds1For prefix, the number in local var is added1Then load it into the stack, and postfix is to first load from the stack local var to the stack, and then add the local var's1, which is equivalent to leaving a backup.

Conclusion:

One. Prefix and Postfix++Both first add the value of the variable1, not prefix++First add1Then calculate, and then post++First calculate and then add1.
Two. In terms of the program, post++First, assign the variable to a temporary variable, and then add the value of the variable1, and then use that temporary variable for the operation.
Three. In terms of instructions, post++Before executing the increment instruction (iinc), the value of the variable is pushed onto the stack, and after executing the increment instruction, the value pushed onto the stack earlier is used.

I hope this article can help you completely understand the prefix++And Rear++Thank you all for supporting this site!

You May Also Like