The diagram above shows how our Java classes are loaded into memory from Java bytecode files, then parsed into the method area from memory, and then used by the process. Compilation of Java domains
Our Java domain initialization is done in two phases, parsing and initializing , where parsing first initializes the base type and string type in the domain to 0 and "", and the reference type in the domain is initialized to null (system default settings). The initialization phase of clinit () and Init () is then initialized (user code settings).
And the domain is divided into static domain and non-static domain.
Static domain: Initialized in clinit (), where the class variable is stored in the method area.
Non-static domain: Initialized in init (), belonging to the instance variable stored in the heap object.
code example:
After compiling the code, you can see the order of initialization:
the timing of code block execution: static code blocks are added to Clinit () during compilation, and non-static code blocks are added to Init () during compilation, and the Order of additions is consistent with the order of code blocks in the code.
come to the conclusion
Only one class is loaded and then the next action is followed by the diagram. And our static domain belongs to the method area, each class corresponds to this one class instance, and our instance object is generated based on this class instance, so the static domain initialization is only once, and our non-static domain is corresponding to the instance , is in the object, that is, a new object is initialized once. compilation of final static domain
Final static static constant field, divided into two cases, the first is the final static modified variable type and the initialization of the data is a constant type and the basic type, then this variable is initialized during compilation, because the compilation process constants are in the constant pool can be determined, Virtual opportunities take these variables as constants, and if they are reference types, then you need to initialize the Clinit (), which is the same as initialization and static initialization.
As shown in the following code:
Compiled code:
We can see that i2 and S2 are not in Clinit (), the other non final static fields are initialized in Clinit (). This distinguishes between the "haha" and the new string ("Heihei"), the former being a constant string, and the latter being an object of reference type. In fact, the class initializes the system defaults in the parsing phase before the initialization step. And our final static modified variable system defaults to what we see as "haha", 2, while the other defaults are null or 0 or "", and then the initialization phase executes clinit () and init () initialization functions.
Conclusion
-The original type and sring type of the final static modifier (not reference type) are not translated into the Clinit method, but are initialized when the Initsfields method is executed before the class initialization step.
-the reference type of the final static modifier, initialization is still in the Clinit method.
Myth: Often see if a field is a constant, then the final static modifier is recommended. It's obvious that this is a problem, and the only thing that's optimized is the original type of final static and the String type field (not reference type), and if it's a reference type, there's no optimization.
When parsing a domain in Java, you must confirm that the class in which the domain is located is parsed, and parsing a class ensures that its parent class is loaded and parsed, that the class is loaded and parsed, then the domain is indexed and then the domain is available. In a hot deployment, our final static domain can be replaced if it is a base type or a string (not a reference type) because constants in the method area can be found to be substituted. Our final static domain is a reference type and is not allowed because the initialization of this fielld is done in Clinit (), and we know that the class will only invoke the Clinit () function once from load to use, so there is no way to modify it. Compilation of Java methods
After the class is parsed, a corresponding virtual method table is generated for the class in the method area. In addition to the internal classes and anonymous inner classes that can create new methods, we find that if obfuscation is applied in the project, it can also lead to the inline and cropping of the method, which in the end results in the addition/reduction of a method, and describes which scenarios cause the methods to be inline and cropped.
The method inline method is not applied anywhere else, there is no doubt that the method will be inline is simple enough, such as the implementation of a method is a line, the method will be inline, then any calls to the method will be replaced by the implementation of the method.
method is only referenced in one place, and this place is replaced by the implementation of the method.
The code example is as follows:
Then you will see that the Print method does not appear when you compile, because it satisfies a condition that is only referenced by one place.
method Cropping
When the parameter of the method is not used, the parameter is cropped.
As shown in the following code:
Compiled code:
You will find that there is no context parameter in the method in the compiled code, and the parameter is cropped off, is there any way to ensure that the method is not cropped out? Of course, the following code:
Note that you cannot use the base type false, you must use the wrapper class Boolean, because if the base type is written, the IF statement may be optimized.
In hot deployment, it is best not to crop and inline, because of the reduction or increase in the method, the clipping leads to an increase in methods, because the new complement Butyrolactone is considered a new method, so it is unified to add new methods; Inline results in method reduction because methods that satisfy the conditions are synthesized into the method being called So that the number of methods is reduced by one. The addition and reduction of the domain/method is not allowed in hot deployment.