Well, it's going to be an interview, and the bottom of Java is definitely something to know. Search the Internet for the bottom of the Java article, make a mark. The Java underlying is primarily the loading, connection, and initialization of classes.
This paper is mainly divided into four aspects:
(1) Java Bottom-level overview
(2) Differences between the new and newinstance () methods
(3) In-depth discussion of Java loading mechanism
(4) The complete process of a full Java program flushing Java xxx.class execution
Four parts are from the online information, four parts read, you should have some understanding of the bottom of Java.
The following text is from "Crazy Java Handout":
1. Loading, connecting, and initializing classes
When a class is actively used by the system, the system loads, connects, and initializes three steps if the class has not yet been loaded into memory.
1. The class's load reads the class file into memory and creates a Java.lang.Class object for it.
The source of the class file:
(1) Loading the class file locally
(2) loading from the jar package (System API)
(3) loading from the network
2. Connection of the class:
The connection phase is responsible for merging the binary data of the class into the JRE.
3. Initialization of classes
Initialization time of the class:
(1) Create an instance of the class: The new operator, the reflection creation instance, the deserialization;
(2) Invoking a static method of a class;
(3) To access the static properties of a class (except the final attribute).
The difference between the 2.new operator and the Newinstance () method
http://lvqingboy-163-com.iteye.com/blog/657599
When initializing a class, generating an instance, the Newinstance () method and the new keyword are the main differences except one method and one keyword. They differ in the way that objects are created, using the class-loading mechanism, which creates a new class. So why are there two ways to create objects? This mainly takes into account the software design ideas of scalability, extensibility and reusability.
Factory mode in Java often uses the Newinstance () method to create objects, so you can find specific answers from why you use Factory mode. For example:
Class C = Class.forName ("Example");
Factory = (exampleinterface) c.newinstance ();
Where Exampleinterface is the interface of example, it can be written as follows:
String className = "Example";
Class C = Class.forName (ClassName);
Factory = (exampleinterface) c.newinstance ();
Further can be written in the following form:
String className = readfromxmlconfig;//getting a string from an XML configuration file
Class C = Class.forName (ClassName);
Factory = (exampleinterface) c.newinstance ();
The above code no longer exists example class name, its advantage is that no matter how the example class changes, the above code is unchanged, can even replace example brothers Example2, Example3, Example4 ... As long as they inherit exampleinterface, they can.
From the JVM's point of view, when we use the keyword new to create a class, the class can not be loaded. However, when using the Newinstance () method, it is necessary to ensure that: 1, the class has been loaded, 2, the class has been connected. And the completion of the above two steps is exactly the static method of Class forname (), this static method calls the Startup class loader, that is, the loader loaded Javaapi.
As can be seen now, newinstance () is actually breaking new into two steps, that is, calling the class load method to load a class first and then instantiating it. The benefits of this step are obvious. We can get better flexibility when invoking the static Load method of class forname, providing a means of decoupling.
Finally, use the simplest description to differentiate between the new keyword and the Newinstance () method:
Newinstance: Weak type. Low efficiency. Only parameterless constructs can be called.
NEW: Strongly typed. relatively efficient. Can invoke any public construct.
My understanding:
In fact, the newinstance () method is explicitly loaded:
The class forname (String s) method loads the custom class TestClass and initializes the instance with the Newinstance () method.
The new operator is implicitly loaded: The JRE loads the corresponding instance class into memory when it executes to the new keyword.
3. In-depth discussion of the Java ClassLoader mechanism
Http://www.blogjava.net/William/archive/2006/08/25/65804.html
The Java language is a dynamic, interpreted programming language that is compiled and generated by the Java Virtual machine when the program is specified to run. class files are loaded into memory according to requirements and rules, and are organized into a complete Java application. The Java language compiles each individual class and interface Implements into a separate one. class files, which are units that can be dynamically loaded for the Java operating environment. It is because of this characteristic of Java that we can compile only the cells that need to be modified without recompiling the other code, and compile the modified files. class file into the Java path, and the next time the Java Virtual machine is reactivated, the logical Java application will be updated with its own functionality because of the new modified. class file loaded, which is the dynamic nature of Java.
Here's a simple example of a basic understanding of the dynamic loading of Java:
[Java]View PlainCopy
- Class testclassa{
- Publicvoid method () {
- System.out.println ("Loadingclassa");
- }
- }
- Public class Classloadertest {
- Publicstatic void Main (String args[]) {
- Testclassatestclassa = new Testclassa ();
- Testclassa.method ();
- }
- }
Post-Compile input command: Java-verbose:class classloadertest, execute file. (java-verbose:class: View the load of Class)
Output structure (1)
Figure (1)
From the running results we can see that the JRE (javaruntime environment) first loads the Classloadertest file and then loads the Testclassa file, thus enabling dynamic loading.
1. Preload and load on demand
Java operating environment in order to optimize the system, improve the execution speed of the program, at the beginning of the JRE run will be the basic class of Java operation required to load the pre-loaded (pre-loading) method of all loaded in memory, because these units in the Java program running process is often used, This includes all the. class files inside the JRE's Rt.jar file.
When the Java.exe virtual machine starts running, it will find the JRE environment installed on the machine, then give control to the JRE, and the JRE's classloader will load the Rt.jar base class file library in the Lib directory into memory, which is necessary for the Java program to execute, so the system is running These files are loaded, avoiding multiple IO operations in the future, thus improving program execution efficiency.
Figure (2) We can see that multiple base classes are loaded, java.lang.object,java.io.serializable, and so on.
Figure (2)
Instead of preloading, we need to use the load-on-demand method (Load-on-demand) when we want to use our own defined classes in our program, which is when Java programs need to be loaded to reduce memory consumption, because the Java language is designed to be embedded in the field.
One more thing to note here is, when does the JRE load on demand actually load the class into the inside?
When we define an instance of a class, such as Testclassa Testclassa, this time the value of Testclassa is null, that is, there is no initialization, there is no call to Testclassa constructor, only if the execution Testclassa = new Testclassa (), the JRE is actually loading the Testclassa.
2. implicit loading and explicit loading
Implicit loading: Using the new operator;
Display load: Use the Forname () Method: Class forname (String s) method to load the custom class TestClass and initialize the instance with the Newinstance () method.
Java loading methods are divided into implicit loading (implicit) and display loading (explicit), the above example is the implicit loading method. The so-called implicit loading is that we define an instance variable with the new keyword in the program, and the JRE loads the corresponding instance class into memory when it executes to the new keyword. Implicit loading method is very common, with a lot of, the JRE system in the background automatically help users to load, reduce the user's workload, but also increase the security of the system and the readability of the program.
The relatively implicitly load is the display load that we don't often use. The so-called display loading is a programmer to write their own program to load the required classes into memory, the following we look at a program:
[Java]View PlainCopy
- Class testclass{
- Publicvoid method () {
- System.out.println ("Testclass-method");
- }
- }
- Public class Cltest {
- Publicstatic void Main (String args[]) {
- try{
- CLASSC = Class.forName ("TestClass");
- Testclassobject = (TestClass) c.newinstance ();
- Object.Method ();
- }catch (Exceptione) {
- E.printstacktrace ();
- }
- }
- }
We load the custom class TestClass through the class forname (String s) method and initialize the instance through the Newinstance () method. In fact, the class has a lot of functions, not detailed here, interested in reference to the JDK documentation.
The forname () method of class has another form: Class forname (String S, Boolean flag, ClassLoader ClassLoader), s means the name of the class that needs to be loaded, and flag indicates that the function is loaded when called Initializes the static zone when the class is initialized, ClassLoader represents the loader required to load the class.
Forname (string s) is called by default through Classloader.getcallerclassloader (), but the method is a private method that we cannot invoke if we want to use class forname (string s, Boolean flag, ClassLoader ClassLoader) to load the class, it is necessary to specify the class loader, which can be implemented in the following way:
Test test = new tests (); The//test class is a custom testing class;
ClassLoader cl = Test.getclass (). getClassLoader ();
Gets the class loader for test;
Class c =class.forname ("TestClass", true, CL);
Because a class needs to have a loader to load, we are loading it by getting the loader CL of the Load Test class as the ClassLoader for loading TestClass.
3. Custom class loading mechanism
We used to call the system's class loader to implement the loading, in fact, we can define the class loader ourselves. This can be achieved using the Java.NET.URLClassLoader class provided by Java. Let's look at an example:
[Java]View PlainCopy
- try{
- Urlurl = New URL ("file:/d:/test/lib/");
- URLCLASSLOADERURLCL = New URLClassLoader (new Url[]{url});
- CLASSC = Urlcl.loadclass ("Testclassa");
- Testclassaobject = (Testclassa) c.newinstance ();
- Object.Method ();
- }catch (Exceptione) {
- E.printstacktrace ();
- }
We implemented the load of the Testclassa class and called the method () methods through a custom class loader. Analyze This program: First define the URL specifies where the ClassLoader loads the class, the URL can point to any location on the internetwork, or it can point to the file system (including the JAR file) on our computer. In the example above, we look for classes from file:/d:/test/lib/, then define URLClassLoader to load the required classes, and finally we can use that instance.
4. Class Loader Hierarchy System
After discussing so much, let's take a closer look at how the Java ClassLoader works:
When the Java ***.class is executed, Java.exe will help us find the JRE and then find the Jvm.dll inside the JRE, which is the real Java virtual machine, finally loading the dynamic library and activating the Java Virtual machine. After the virtual machine activates, it will do some initialization actions, such as reading the system parameters. Once the initialization action is complete, the first class loader ――bootstrap Loader, Bootstrap Loader is written by C + +, this Bootstrap Loader the initial work, in addition to some basic initialization action, the heaviest All you have to do is load the Extclassloader in Launcher.java and set its parent to null, which means it is bootstraploader. The Bootstrap Loader then asks to load the Appclassloader in Launcher.java and sets its Parent as the Extclassloader entity that was previously generated. Both loaders exist in the form of static classes. It is to be noted here that Launcher$extclassloader.class and Launcher$appclassloader.class are loaded by Bootstrap Loader, so the Parent It is not related to which class loader is loaded.
The following graphs can represent the relationships between the three:
Parent class
Parent class
Gta5-In
Gta5-In
Bootstraploader
PARENT
Appclassloader
PARENT
Extclassloader
These three loaders constitute our Java class loading system. They look for the classes required by the program from the following paths:
BootstrapLoader:sun.boot.class.path
ExtClassLoader:java.ext.dirs
AppClassLoader:java.class.path
These three system parameters can be obtained by the System.getproperty () function to obtain a specific path. You can implement your own programming to see the specific path.
5. Summarize
Understand the Java class loading mechanism for our skilled and flexible use of the Java language, improve the efficiency of the program has a very important role, know it should also know why, so as to improve the quality of the program as a whole.
4. Drill down into the Java bottom
Http://bigwhite.blogbus.com/logs/579744.html
In a friend's bookshelf found Sen Wang "Java Deep Adventure" book, read the preface to understand the book is about the Java underlying technology insider. With a curious glance, there is not much to gain, but also to write down some of the two points that I think are useful.
Java xxx
What happens when we tap on the command line: "java XXX"?
The process is as follows:
1. Locate the JRE;
2. Find JVM.dll;
3. Start the JVM and initialize it;
4. Produce bootstrap Loader;
5. Loading extclassloader; (ext–extended)
6. Loading into Appclassloader;
7. Load the XXX class.
The book mentions that Bootstrap Loader, Extclassloader and Appclassloader constitute Java's "ClassLoader Inheritance system--class Loader hierarchy", where Bootstraploader is a C + + written, the other two are written by Java. The reason for being an "inheritance system" is that there are links between the three loader. Bootstrap Loader is responsible for loading the extclassloader, which extclassloader the parent to Bootstrap Loader. Appclassloader is special, although it is loaded by Bootstrap, but its parent is set to Extclassloader. The reason for this is to implement the "delegate model". The "delegate model" is simply a request that when the ClassLoader has the load class, it asks its parent to use its search path to help load, and if its parent cannot find it, it uses its own search path to load. As stated above, when Extclassloader wants to load the Appclassloader class, it first requests its parent "Bootstrap Loader" to help, Bootstrap Loader will load Appclassloader, Since this loading is Extclassloader request, the parent of Appclassloader is still set to Extclassloader instead of Bootstrap Loader.
The loading process of the class
Class loading follows a principle: "The ClassLoader will load from top to bottom according to the class's inheritance system." For example: "If C Inherits B and implements interface I, and B has inherited from a", then the class loader loads C, the order of loading is a-> b->i->c, (Note: interface will be compiled as class by the Java compiler as a standalone. class file)
In fact, I do not give too much attention to the underlying things, if in which project needs me to understand the bottom, I will go very good study.
Well, by the way, look what's up. dll
Http://baike.baidu.com/view/887.htm
Dynamic link library English is a DLL, is an abbreviated form of the Dynamiclink library, a DLL is a library containing code and data that can be used by multiple programs at the same time, DLL is not an executable file. Dynamic linking provides a way for a process to invoke a function that is not part of its executable code. The executable code of the function is in a DLL that contains one or more functions that have been compiled, linked, and stored separately from the processes that use them. DLLs also help to share data and resources. Multiple applications can access the contents of a single copy of a DLL in memory at the same time. A DLL is a library that contains code and data that can be used by multiple programs at the same time.
Java Underlying learning