java ~ Introduce the three major features and guardians of the thread

2023-01-23   ES  

1. Java’s reflection mechanism

Each Java program must be compiled, loaded, connected, and initialized before the execution of the Java program. The latter three stages are as follows:


i, loading refers to the binary data in the compiled Java file (that is, the .class file) in the compiled. . Lang.Class objects are used to encapsulate the data structure of the method area.is that the Class object is finally obtained after loading, and what is more noteworthy is:java.lang.class object issingle instance
, no matter how many objects of this class created, his Class object was the only one! Intersection Intersection Intersection EssenceLoad and obtain the Class object through three ways:
class.Forname (full path of class), instance object.class (attribute), instance object getClass ()
. The difference between them will be mentioned below! Intersection Intersection

### Another, the static code block in the class class will be executed(For details, see the previous blog: class.Forname () Load the JDBC driver)

II, in the connection and initialization stage, in fact, the static variables have been assigned twice: the first time is the default value of the static variable type; the second is the value we really assign to the static variable.
III and Java are divided into two ways: active and passive use. Actively use the following figure:


initialization timing is exactly the java program pairfor the first timeUse, except for the above 6 methods, the use of other classes is passively used, which will not lead to the initialization of the class. And should pay attention to the following aspects:

interfaces: can be used as a class (because he can be initialized when there is a static variable in the interface); the interface is the interface, which is not related to the class (there is no structure in the interface, so it cannot be initialized) cannot be initialized)

2, class.Forname, instance object.class (attribute), instance object getClass ()

1. Same points:


Through these methods, the Java.lang.class objects are obtained (this is the final product obtained when the class mentioned above was loaded)
For example:
package demo;

public class A

{



public static void main(String[] args) throws Exception



{



 


system.out.println (a.class); // Obtain class object through the class .class



 


A a = new A();



 


system.out.println (a.getclass ()); // Obtain the class object through the instance name .GetClass ()


 


system.out.println (Class.Forname (“Demo.a”); // Obtain the Class object through class.Forname






 

 
System.out.println(“……………………………”);



 

 
system.out.println (a); // Use different ways to create objects


 

 
System.out.println(A.class.newInstance());



 

 
System.out.println(a.getClass().newInstance());



 

 
System.out.println(Class.forName(“demo.A”).newInstance()); 




}

}

result: Class demo.a




class demo.A





class demo.A


(Here you can also get a class Class object is the only one)

…………………………………





[email protected]





[email protected]





[email protected]





[email protected]

2, the difference:

1)Class cl=A.class; JVMwill use classA,AInstant memory

The premise of(:classAHaven’t installed memory yet),No classificationADo the initialization of class.Return classAClassobject

2)Class cl=object quotationo.getClass();Return to referenceoThe object that really refers to(because of:The reference of the son object may be given to the reference variables of the father object)Classobject

3)Class.forName(“class name“); JAVAEveryone knows.A,and initialization of class

Attachment:

From the angle ofJVM, we use keywordsnewWhen creating a class, this class may not be loaded. Useclass object’s newInstance ()

When themethod, it must be guaranteed:1, this

Classhas been loaded;2, this class has been connected. And the two steps to complete the above steps are exactly the two stepsClassStatic methodforName(), this static method calls the startup class loader, that is, loading

The loader ofjava API.

can now see that the class objectnewInstance()(This usage and the factory mode in Java have the same wonderful work) actuallynewThis method is decomposed into two steps, that is, first callClassLoad the method to load a certain class, and then instantiated.

The benefits of This step is obvious. We can callclassStatic loading method

Getting better flexibility atforNameprovides a means of coupling.

Finally distinguish with the simplest descriptionnewKeywords andnewInstance()Method difference:
newInstance: weak type. ineffective. You can only call the non -clicter structure.
new: strong type. Relatively efficient. Can call anypublicStructure.

source

Related Posts

python Save JSON data, store in txt file

POJ 3189 Steady Cow Assignment Maximum Stream

PTGAN with conversion model and segmentation model

LUA grammar Quick Getting Started (5 minutes to learn Lua)

java ~ Introduce the three major features and guardians of the thread

Random Posts

bzoj 4198 [noI2015] Homer epic

NASM source code analysis pre -processing (1)

What is Python programming language?

The QSORT function of Reference C language implements a function that sorts the array that can sort any data type (including the structure) (inside is not fast sorting) Captain

Android drawer effect implementation