I have been learning Java, encountered many problems, encountered many problems about I ++ and ++ I, and the most classic string STR = "ABC"
We have created several objects in a group. One day I learned about the Java disassembly command
Javap. Now we will make a summary of the learning records for you to refer to later. If any error occurs, correct it.
1. What is javap:
Where options include:
-C disassemble the code
-Classpath <pathlist> specify where to find user class files
-Extdirs <dirs> override location of installed extensions
-Help print this usage message
-J <flag> pass <flag> directly to the runtime system
-L print line number and local variable tables
-Public show only public classes and members
-Protected show protected/public classes and members
-Package show package/protected/public classes
And Members (default)
-Private show all classes and members
-S print internal Type Signatures
-Bootclasspath <pathlist> override location of class files loaded
By the bootstrap Class Loader
-Verbose print stack size, number of locals and ARGs for met
HODs
If verifying, print reasons for failure
The above is the description of it in Baidu encyclopedia. It only introduces some javap parameters and usage methods, and we need to use this one:-C disassemble the code.
Clarify the question: What is javap?
Someone on the Internet calls it the anti-assembler. You can view the bytecode generated by the Java compiler for us. Through it, we can compare the source code and bytecode to learn a lot about the internal work of the compiler.
2. A preliminary understanding of javap
Start with a simple example:
In this example, we simply declare two int variables and assign the initial values. Next let's take a look at what javap has brought to us: (of course, before executing the javap command, you must first configure your own environment and use javac to compile the environment, that is:Javac testjavap. Java
)
Let's just read it (for convenience, write the comments to the end of each sentence)
Code:
0: iconst_2
// Place 2 to the top of the stack
1: istore_1
// Place the value at the top of the stack to the local variable 1, that is, in I.
2: iconst_3
// Place 3 to the top of the stack
3: istore_2
// Place the value at the top of the stack to local variable 1, that is, J.
4: Return
Is it easy? (Of course, it is estimated that the knowledge of data structures is required.) let's add the Java knowledge about the stack:
For int I = 2; first, it creates a reference with the variable I in the stack, and then finds whether there is an address with the nominal value 2, not found, open up an address that stores the nominal value of 2, and then point I to the address of 2.
After reading this section and comparing the above comments, is it totally consistent?
To verify the above statement, we continue the experiment:
We set the values of I and J to 2. According to the above theory, when J is declared, there will be no address with a nominal value of 2 in the stack, because there is already 2 in the stack, direct J to the address 2. In this case, both I and J point to 2 at the same time.
Decompile javap-C: The result is as follows:
Code:
0: iconst_2 // place 2 to the top of the stack
1: istore_1 // place the value at the top of the stack to local variable 1, that is, I.
2: iconst_2 // place 2 to the top of the stack
3: istore_2 // place the value at the top of the stack to local variable 2, that is, J (I and j both point to 2)
4: Return
Although I and j point to 2 at the same time, it does not mean that I and J point to the same address (Java does not allow programmers to directly modify the data in the stack, so don't think about it, can I?
Modify 2 in the stack, so that the values of I and j will not change. In addition, in the compiler, when J = 2; is encountered, it will re-search whether there is a 2-character nominal value in the stack, if not, re-open the address to store 2
Value; If yes, direct J to this address. Therefore, even if J is assigned another value, such as J = 4, the change of J value does not affect the value of I .)
Another example:
Or javap-C
Code:
0: iconst_2// Place 2 to the top of the stack
1: istore_1// Place the value at the top of the stack to the local variable 1, that is, in I.
2: iload_1//
Place the I value on the top of the stack. That is to say, the value at the top of the stack is 2.
3: istore_2// Place the value at the top of the stack to local variable 2, that is, J.
4: Return
Is it clear here?
Since we have some knowledge about javap, we can use it to solve some practical problems:
1. I ++ and ++ I Problems
Decompilation result:
Code:
0: iconst_1
1: istore_1
2: iinc 1, 1 // This command adds 1 to the local variable 1, that is, I. This command does not cause stack changes. I is changed to 2 at this time.
5: iconst_1
6: istore_2
7: iinc 2, 1 // This command adds 1 to the local variable 2, that is, J. This command does not cause stack changes. J is changed to 2 at this time.
10: Return
It can be seen that ++ is behind and there is no difference in this Code.
Let's look at another piece of code:
Decompilation result:
Code:
0: iconst_1
1: istore_1
2: iload_1
3: iinc 1, 1 // The local variable 1 (I) plus 1 to 2. Note that the stack is still 1 and has not changed.
6: istore_1 // place the value at the top of the stack to the local variable 1, that is, I changes from 2 to 1 at this time.
7: iconst_1
8: istore_2
9: iinc 2, 1 // The local variable 2 (j) plus 1 becomes 2. Note that the stack is still 1 and has not changed.
12: iload_2 // place the value of local variable 2 (j) to the top of the stack. At this time, the value of the top of the stack changes to 2.
13: istore_2 // place the value at the top of the stack to local variable 2, that is, J is actually changed from 1 to 2 at this time
14: Return
Do you understand? If you understand this, the following problem should be solved:
M = m ++; In this statement, the Java virtual machine executes this: The m value is added with 1, but the value in the stack is still 0. The value in the stack immediately overwrites M, that is, M is changed to 0, so no matter how many cycles, M is equal to 0.
If it is changed to M = ++ m, the program runs 100...