In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the Jvm class file how to load, initialize, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor with you to understand.
When the written java file is actually run, it is first compiled into a binary file ending with ".class" and then loaded by the virtual machine. Then a class file in a virtual machine needs to go through several steps to become an java instance:
I. steps for a class file to become an instance of java
1. Load: the mount phase is completed by three basic actions. To mount a type, the java virtual machine must:
(1) A binary data stream representing the type is generated through the fully qualified name of the type.
(2) parse the binary data stream into the internal data structure in the method area
(3) create an instance that represents this type of java.lang.Class
* when creating an object with the new keyword at that time, the data structure of the class is stored in the method area, and the objects out of new are stored in the heap
Binary data streams can be generated in the following ways:
1. Load a java class file locally
2. Download a class file on the network
3. Extract class files from a zip, jar, or other document.
2. Verification: after the class is loaded, it is necessary to prepare the connection. The first step of the connection is to verify that the type conforms to the java language specification and does not endanger the integrity of the virtual machine.
Type checking-make sure that every class except Object must have a superclass and that all superclasses of that class have been loaded
Binary compatibility check between classes-check that final classes cannot be used for subclasses, check that methods of final classes cannot be overridden, and ensure that there are no incompatible methods between subclasses and superclasses
-- check that all constant pool entries are consistent with each other
-- check whether all special strings in the constant pool are formatted
Check the integrity of the bytecode (the most complex step)
3. Preparation: in the preparation phase, the java virtual machine allocates memory for the class variables and sets the default initial values, but the class variables are not initialized to the real initial values before the initialization is reached. That is, we declare int a = 3 in the class, but in this step, the default value of a to the assigned type, 0 int a = 0, does not support the boolean type. Internally, the boolean variable is set to 0 of the int type by default, that is, initialized to false.
4. Parsing: after verification and preparation, we enter the parsing process. Parsing is the process of finding symbolic references to classes, interfaces, fields, and methods in the constant pool of types, and replacing these references with direct references.
5. Initialization: initialization is to give a variable a real initial value
For example, we define private static int axi1; at this time, we assign a value of 1
Second, dynamic linking and analysis
Class files keep all symbolic references in a constant pool, and each class file has a constant pool. Each class or interface loaded by the virtual machine has an internal version of the constant pool, which is called the runtime pool.
Constant pool parsing-when a program is running, a specific symbolic reference must first be parsed to be used. The parsing process is to find the entity according to the symbolic reference and replace the symbolic reference with a direct reference. Each symbol reference is parsed only once
Early parsing-all symbolic references are parsed in advance, from the initial class to the subsequent classes, until all symbolic references are resolved.
Late parsing-parsing at the last minute of accessing each symbol reference. (you can also choose a compromise between the two situations)
-- Program execution throws an error only when it actually accesses a symbol reference for the first time. To the user, it seems to be too late to parse.
The java virtual machine processes all string words with the same string order into a String object. That is, if multiple classes use the same string "Hello", the java virtual machine will only create a String object with a "Hello" value to represent all the string literals.
-- the value of any byte, short, char rebate www.fx61.com, when pushed into the stack, will be converted to int first.
-- the operations involving byte, short and char will first convert them to int type, calculate them and then get the int result. If you need byte and other results, you need to perform display conversion.
Memory in the java virtual machine can only be allocated in the heap in the form of objects. Consider basic type wrappers if necessary.
The synchronization mechanism used by java is the monitor, which in java supports two types of threads: mutex and collaboration. Java virtual machines are mutually exclusive through locks, and collaboration is achieved through Object's wait and notify methods.
Notify should be used only when it is absolutely certain that only one thread in the waiting area is suspended, and notify all should be used as long as there is the possibility of multiple threads hanging at the same time. Otherwise, it may cause a particular thread to wait too long in the waiting area, or even never wake up.
The heap and method area are shared by all threads
-- two kinds of data that will be accessed by multithreading: instance variables stored in the heap and class variables stored in the method area
Variables that do not need to be protected: local variables in the java stack that are private to the thread that owns the thread
When the virtual machine loads a class file, an instance of the java.lang.Class class is created. When you lock a class, you lock the Class object of that class.
3. Three types of loaders built into jvm
BootStrap class loader (BootStrapClassLoader): root class loader. The loader does not have a parent loader and is responsible for loading the core class libraries of the virtual machine, such as java.lang.*, etc. Java.lang.Object is loaded by the root loader
Extension classloader (ExtClassLoader): its parent is the root loader, which is the one above. It loads the class library from the directory specified by the java.ext.dirs system properties, or from the jre/lib/ extsubdirectory of the jdk installation directory. This class is a pure java class and a subclass of the lava.lang.ClassLoader class
The parent of the System system classloader (AppClassLoader), also known as the application classloader, is the extended classloader, the one above. It is loaded from the directory specified in the environment variable classpath. It is the default parent loader for the user-defined classloader, which is a pure java class and a subclass of the lava.lang.ClassLoader class
Note: when using Class.forName ("com.test.Test1") for class loading, static blocks of code in the class are automatically executed, but no constructors are executed
When loading using loadClass ("com.test.Test1"), static blocks of code are not automatically executed, nor are constructors executed
* *: the same class, loaded by two different class loaders, will be considered to be two different classes, and there will be two copies of the class information in the method area.
Only classes loaded using the startup class loader, such as java.lang.Strong, java.lang.Object, and so on, have only one copy of the class information in the method area.
The basis for judging whether two classes are equal is whether the class loaders of the two classes are the same.
Initialization: for the initialization phase of a class, the virtual machine specification specifies that the class must be initialized immediately in five cases.
1. When you encounter the four bytecode instructions of new, getstatic, putstatic and invokestatic, the class will be initialized if it has not been initialized before. These four bytecodes correspond to: new instance, static field value, static field assignment, static field call.
2. Use the method of java.lang.refleat package to make reflection call
3. When initializing a class, if you find that its parent class has not been initialized, you need to initialize its parent class first
4. When the virtual machine starts, the class that executes the main method is required.
5. When using LDK1.7 's dynamic prophecy support, if the final parsing result of a java.lang.invole.MethodHandle instance is the method handle of REF_getStatic,REF_putStatic,REF_invokeStatic.
The following situations need to be noted:
1. For static fields, only the class or interface that directly defines the field will be initialized. If the static field in the parent class is referenced through a subclass, only the initialization of the parent class will be triggered.
2. Constants are stored in the constant pool of the calling class during the compilation phase, which is essentially not directly referenced to the constant of the defining class, so the initialization of the class that defines the constant will not be triggered. Note: only the constant that the compiler can determine will do this operation, and the constant value that the compiler cannot determine will trigger the initialization of the target class.
For example: public static final String uuid = UUID.randomUUID (). ToString (); this line of code, when other classes refer to uuid, triggers the initialization of the class that defines the uuid
3. Referencing a class through an array definition does not trigger the initialization of the class, for example: Super [] s = new Super [10]; this line of code does not trigger the initialization of the Super class
4. When an interface is initialized, the parent interface is not required to be initialized, but only when the parent interface is actually used.
IV. The difference between case class and class
1. When initializing, you don't need new. Of course, you can also add it. Ordinary classes must add new.
Class ABC (name:String) {
Def ff (): Unit = {
}
}
Case class ABC1 (name:String) {
Def ff1 (): Unit = {
}
}
Val abc1=ABC1 ("fg")
Val abc= new ABC ("xx")
2. The implementation of toString is more beautiful
Println (abc1.toString)
Println (abc.toString)
3. Equals hashcode is implemented by default
4. The default is serializable, that is, Serializable is implemented.
5. Automatically inherit some functions from scala.Producet
6. The parameters of the case class constructor are at the publiec level, so we can directly access the
7. Support pattern matching
Thank you for reading this article carefully. I hope the article "how to load and initialize class files in Jvm" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.