About Java interview knowledge base articles to resolve --JVM

Quit happen all the time, but I suggest that you quit before, to make it clear why quit. Must not follow suit, to see my colleagues one by one are gone, and he began to interview them blind (also not prepared adequately period), in the end because of technical reasons (the impact of their development, offset path planning of their own), or the money to less, unappreciated.

Inadequate preparation of the interview, complete waste of time, it is not responsible for their own (if the title is high, when I did). Under today to share knowledge Java interview resolve --JVM Basics

1) Java is how to achieve cross-platform?

Note: Java is a cross-platform program, rather than the JVM. JVM is using C / C ++ development, machine code is compiled, you can not cross-platform, under different platforms will need to install a different version of the JVM

A: We have written in the Java source code, the compiler will produce a .class file, called a byte code file. Java Virtual Machine (JVM) is responsible for translating the byte code into machine code in a file and then run the platform-specific, that is, as long as the corresponding JVM installed on different platforms, you can run byte code file, we write operation Java programs.

This process, Java programs we wrote did not make any changes, just the JVM through the "middle layer", will be able to run on different platforms, so that "once, run everywhere" purposes.

2) What is the JVM?

Analysis: the role of not only the basic concepts, as well as the JVM.

A: JVM, namely Java Virtual Machine, Java virtual machine. It is achieved by a computer simulation of a computer having a calculating function. Computer architecture is possible across JVM to execute Java byte code, the shielding is mainly due to the difference between the JVM software or hardware associated with each computer platform, such that the coupling to provide a unified platform associated with those achieved by the JVM.

3) JVM What are the components?

Analysis: This is a study of the architecture of the JVM

A: the JVM structure essentially consists of four parts:

Class loader, will need to start the JVM runtime class or class is loaded into the JVM

Execution engine, task execution engine is responsible for implementing the class bytecode instructions contained in the file, which is equivalent CPU on the actual machine

Memory area, the memory is divided into several zones in analog storage, scheduling and recording functional module on real machines, such as PC or the recorder pointer register various functions on an actual machine, etc.

Local method calls, method calls the C or C ++ native code returns the results achieved

4) class loader is to understand it?

Analysis: investigation of the underlying principles, which concept relates to the class loader, as well as some functions of the underlying implementation.

A: As the name suggests, the class loader (class loader) to load Java classes into the Java virtual machine. In general, Java Virtual Machine using Java classes as follows: Java source (.java files) are converted into Java byte code (.class files) After Java compiler.

Class loader is responsible for reading Java byte code, and converted into an instance of class java.lang.Class. Each such instance of a Java class is used to represent. By this example newInstance () method can be used to create an object of that class. The actual situation may be more complex, such as Java byte code may be dynamically generated by the tool, or it may be downloaded through the network.

Interviewer: Java Virtual Machine Java is how to determine the two classes are the same?

A: Java virtual machine not only look like full name is the same, but also the class loader loads such as whether. Only two things being equal, we considered two classes are the same. Even the same byte code, after being loaded by different class loaders obtained class is different. For example, a Java class com.example.Sample, Sample.class generates bytecode file after compilation. Two different classloader ClassLoaderA respectively read and ClassLoaderB Sample.class this document, and define two instances of class java.lang.Class to represent the class. These two examples are not the same. For the Java Virtual Machine, they are a different class. Trying to assign each of these two classes of objects, the exception ClassCastException will be thrown at runtime.

How 5) class loader is to load the class file?

A: The step is shown in the FIG ClassLoader loading a class file to the JVM you need to go through when:

The first stage is to find the .class file and this file contains the byte code is loaded into memory

The second stage can be divided into three steps, namely bytecode verification, linked Class class data structure analysis and the corresponding memory allocation table and the last symbol

The third phase is the static properties and the initialization class assignment, and performing a static block, etc.

Interviewer: Can you elaborate on it?

answer:

1. Load

The first phase to find and load the class of binary data loaded class load in the loading phase, the virtual machine needs to do three things:

To obtain a binary byte stream, which is defined by the fully qualified name of a class.

This represents the byte stream of static storage structure into a run-time data structure area method.

Generating a representative of the Java heap java.lang.Class object of this class, such as a method of access to the data area entry.

Relative to other stages of class loading, loading phase (specifically, the operation is a binary stream of bytes acquired class loading phase) is controllable strongest stage, because the developer can use the system to provide both class loader is to finish loading, you can also customize your own class loader to finish loading.

After loading phase is completed, the external storage format of virtual machines on a binary stream of bytes required for virtual machines in accordance with the method area, but also create a class java.lang.Class objects in the Java heap, so that we can through the these data access objects area.

2. Connect

Verification: ensure the correctness of the loaded class

The first step is to verify the connection phase, the purpose of this stage is to ensure that the information byte stream file included in Class meet the requirements of the current virtual machine and the virtual machine will not jeopardize their own safety. Validation phase will be completed roughly an inspection operation four stages:

File format validation: validation of a byte stream for compliance with Class file format; for example: whether the beginning 0xCAFEBABE, major and minor version number in the current processing range of the virtual machine, if there is the constant in the constant pool type is not supported .

Metadata authentication: described bytecode information semantic analysis (Note: the semantic analysis phase contrast javac compiler), which describes the information to ensure compliance with the requirements of the Java language specification; for example: whether the class has a parent class, except java .lang.Object outside.

Bytecode verification: the data flow and control flow analysis to determine the semantics of the program is legitimate, logical.

Verification of symbolic references: to ensure that the analysis operation is performed properly.

Validation phase is very important, but not necessarily, has no effect on the program run, if the referenced class after repeated verification, consider using -Xverifynone parameter to shut down most of the class verification measures to reduce virtual machine class loading time.

Preparation: allocate memory for the class static variable, and initializes its default values

Preparation phase is formally allocate memory for class variables and set the stage class variable initial values, this memory will be assigned in the method area. The following points need to pay attention to this stage:

This memory allocation time ① includes only class variables (static), and does not include the instance variables, instance variables with a distribution object when the object is instantiated in the heap Java.

② where the initial value is typically set here is the data type of the default value of zero (e.g., 0,0L, null, false, etc.), rather than the values ​​are given explicitly in Java code.

Suppose variable is defined as a class: public static int value = 3;

Then the variable value after the initial preparation phase is 0, instead of three, because this time has not yet started any Java method, and the value assigned public static-command after the program is compiled, stored in the class constructor <clinit among the> () method, so the value will be assigned to action 3 will be performed during the initialization phase.

There is also need to pay attention to the following points:

The basic data types, for the class variable (static) and global variables, if not explicitly assign its direct use, the system will assign it a default value of zero, and for the local variables, before use you must explicitly assign it, or do not compile time.

For while the modified static and final constants, it must be explicitly assigned at the time of its declaration, otherwise it does not compile time; only the final modification of the constants you can either explicitly assign it in the statement, also It can be explicitly assigned values ​​in the case of initialization, in short, before use must explicitly assign their system does not give them a default value of zero.

Reference is a reference to the data type, such as an array reference, references to other objects, if not explicitly be used directly assigned a value of zero for the system will be given a default, i.e. null.

If no value is assigned to each element in the array during array initialization, wherein the element will then be given a default value of zero in accordance with the corresponding data type.

③ If there ConstantValue class attribute field attribute table field, i.e. simultaneously and final static modification, then the preparation stage variable value is initialized to the value specified by the property ConstValue.

The above value is assumed that class variable is defined as: public static final int value = 3;

Compile time Javac will generate ConstantValue property value, in the preparation phase of the virtual machine will be based ConstantValue setting value assigned to 3. We can be understood as static final constants at compile time will result into a constant pool to call its class

Analysis: The class is converted into direct reference symbol references

The resolution phase is the constant pool of the virtual machine process is replaced symbolic references direct reference, analysis operation for the main qualifier class or interface, fields, methods class, interface method, type method, and calls the method handle 7 points for the class reference symbols. Symbolic reference is a set of symbols to describe the target can be any literal.

Direct reference is the direct pointer to the object, an indirect or offset relative to the positioning target handle.

3. Initialize

Initialization, given the correct initial value for the class of static variables, JVM classes responsible for initializing the main class variables are initialized. Initial value set in the Java class variable in two ways:

① declare a class variable is specified initial value

② using static code block is assigned an initial value for class variables

JVM initialization step

1, if the class has not been loaded and connected, and then connected to a program loads the class

2, if the direct parent class has not been initialized, it first initializes its direct parent

3, if the class initialization statements, the system sequentially performs the initialization statements

Class initialization timing: only when the active use of the class initialization will lead the class, the class actively used include the following six:

Create an instance of the class, which is the new way

Access static variables of a class or interface, or assignment of the static variables

Static method call class

Reflector (e.g. Class.forName ( "com.shengsiyuan.Test"))

Initialize the subclass of a class, the parent class will be initialized

When the Java virtual machine started to be marked to start class class (JavaTest), to run a master class directly java.exe command

End of life

In the following situations, Java virtual machine will end the life cycle

Implementation of the System.exit () method

Normal program execution ends

Encountered an exception or error during execution aborted

Because the operating system error caused the process to terminate the Java Virtual Machine

6) the parent delegation model (Parent Delegation Model)?

Analysis: loading class parents adopt delegated mechanisms, security mechanism that can better guarantee the Java platform

A: ClassLoader class loader having a hierarchical structure, i.e. parent-child relationship, wherein, based on Bootstrap loader is the father of all, as shown below:

This model requires addition top Bootstrap class loader boot class loader, the rest of the class loader should have its own parent class loader. Child and parent class loader class loader is not in succession (Inheritance) relationship is achieved, but the parent code with the multiplexed by combining the loader (Composition) relationship. Each class loader has its own namespace (of the loader and all the parent class loader loaded classes in the same namespace, class name does not appear complete (including package of) the same the two classes; in a different namespace, there may be a full class name (including package of) two of the same class)

Interviewer: parents delegate the work process model?

answer:

1. Does the current ClassLoader first queries from his already loaded class such has been loaded, if already loaded directly back to the original already loaded class.

Each class has its own loader loads the cache, when a class is loaded into the cache will be later,

So next time you can load directly returned.

2. The current ClassLoader is not found in the cache loaded class, commissioned by the parent class loader to load, the parent class loader using the same strategy, first check its cache, then delegate the parent class of the parent class to load, has been to bootstrap ClassLoader.

When all of the parent class loader are not loaded, and then loaded by the current class loader, and placed in its own cache, so next time there is a load request return directly.

Interviewer: Why do you design?

Analysis: This is the advantage of using this model to organize accumulator

A: The main is for security, user-written class to avoid some of the core Java class dynamic replacement, such as String, but also to avoid repeated loading, because the JVM to distinguish between different classes, not just the class name, the same class file It loaded a different ClassLoader is two different classes, if mutual transformation, then will throw java.lang.ClassCaseException.

About Java interview knowledge base articles to resolve --JVM

About Java interview knowledge base articles to resolve --JVM
This information is Xiao Bian recently spent several months time to sort out, want to get the above information, click on the link https://jq.qq.com/?_wv=1027&k=5CrpwVu to get free

Guess you like

Origin blog.51cto.com/14416052/2420852