preface

If I ++ and ++ I were just general knowledge

  • I ++, I assign I and then I ++
  • ++ I ++ and then assign

But this simple answer won’t catch the interviewer’s eye, and when analyzed using Java bytecode instructions, it will.

Code implementation

Public class OperandStackTest {/** public static void add(){// public static void add(){// public static void add(); i1++; System.out.println(i1); //11 int i2 = 10; ++i2; System.out.println(i2); // int i3 = 10; int i4 = i3++; System.out.println(i3); //11 System.out.println(i4); //10 int i5 = 10; int i6 = ++i5; System.out.println(i5); //11 System.out.println(i6); Int i7 = 10; // int i7 = 10; i7 = i7++; System.out.println(i7); //10 int i8 = 10; i8 = ++i8; System.out.println(i8); // int i9 = 10; int i10 = i9++ + ++i9; //10+12 System.out.println(i9); //12 System.out.println(i10); //22 } public static void main(String[] args) { add(); }}Copy the code

The results

Bytecode instruction

Run the class file name in the javap -v out directory on the terminal to obtain the following result

 public static void add();
    descriptor: ()V
    flags: ACC_PUBLIC, ACC_STATIC
    Code:
      stack=2, locals=10, args_size=0
         0: bipush        10
         2: istore_0
         3: iinc          0, 1
         6: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
         9: iload_0
        10: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        13: bipush        10
        15: istore_1
        16: iinc          1, 1
        19: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        22: iload_1
        23: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        26: bipush        10
        28: istore_2
        29: iload_2
        30: iinc          2, 1
        33: istore_3
        34: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        37: iload_2
        38: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        41: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        44: iload_3
        45: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        48: bipush        10
        50: istore        4
        52: iinc          4, 1
        55: iload         4
        57: istore        5
        59: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        62: iload         4
        64: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        67: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        70: iload         5
        72: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        75: bipush        10
        77: istore        6
        79: iload         6
        81: iinc          6, 1
        84: istore        6
        86: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
        89: iload         6
        91: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
        94: bipush        10
        96: istore        7
        98: iinc          7, 1
       101: iload         7
       103: istore        7
       105: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
       108: iload         7
       110: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
       113: bipush        10
       115: istore        8
       117: iload         8
       119: iinc          8, 1
       122: iinc          8, 1
       125: iload         8
       127: iadd
       128: istore        9
       130: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
       133: iload         8
       135: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
       138: getstatic     #4                  // Field java/lang/System.out:Ljava/io/PrintStream;
       141: iload         9
       143: invokevirtual #5                  // Method java/io/PrintStream.println:(I)V
       146: return
Copy the code

Explain the above results

The first kind of question

The corresponding instruction is

I1 is pushed with a value of 10 (bipush), then a value of type int is pushed from the stack to the local variable position 0, then iInc is executed to add the value of position 0 +1, and then the local variable position 0 is pushed to the output operation

So the value of i1 is 11

I2 is pushed with a value of 10 (bipush), then a value of type int is pushed from the stack to the local variable position 1, then iInc is executed to change the value of position 1 +1, and then the local variable position 1 is pushed to perform the output operation

So the value of i2 is 11

conclusion

Since there is no assignment, there is not much difference.

Problem of the second kind

I3 is first pushed to the location of local variable table 2, and then it is pushed. Execute iinc to increase the value of position 2 by one, and I4 is stored to the location of local variable table 3

So i3 is 11, i4 is still 10

I5 is pushed and stored at the location of local variable table 4. Since it is ++ I, iInc first increments the value of position 4 by one, and then pushes the value of local variable table 4 to perform the assignment operation, so both are 11

The third category of questions

I7 is pushed first, and then stored on local variable table 6. I6 is pushed first, and then the value at 6 is incremented by one. Since this value is stored on local variable table 6, overwrite is generated and the value is changed to 10.

++ I does not create overwrite by performing the increment and then pushing the value onto the stack, in the assignment to the local variable table, so i8 is 11.

The fourth category of questions

I9 =10 is pushed first, and then there is the position of local variable table 8

int i10 = i9++ + ++i9;
Copy the code

Iload pushes i9 at 8 and then iInc increses i9 at 8 and then ++i9, increses i9 at 8

At this point, i9=10+1+1 is 12, and then i9 at position 8 is pushed onto the stack. Execute add to add the two i9 in the stack and store the obtained value to the position of local variable table 9

So i10=10+12(i9++ is still 10,++i9 is 12, because I did iinc twice)

The virtual and static methods are then called, and the output statement is executed by pushing the value at 9 onto the stack

Original link: blog.csdn.net/demo_yo/art…

Copyright notice: This article is an original article BY CSDN blogger “Unknown Coder”. It is in accordance with CC 4.0 BY-SA copyright agreement. Please attach the original source link and this statement.

Recent hot articles recommended:

1.1,000+ Java Interview Questions and Answers (2021)

2. Don’t use if/ else on full screen again, try strategy mode, it smells good!!

3. Oh, my gosh! What new syntax is xx ≠ null in Java?

4.Spring Boot 2.5 is a blockbuster release, and dark mode is exploding!

5. “Java Development Manual (Songshan version)” the latest release, quick download!

Feel good, don’t forget to click on + forward oh!