Java Common knowledge Collation

Source: Internet
Author: User

First, ClassLoader

When running a Java program, the JVM starts, runs Bootstrap ClassLoader (starts the ClassLoader), loads the Java core API, and then calls Extclassloader to load the extension API. Finally Appclassloader loads the class defined under the Classpath directory, which is the most basic loading process for a program.

The two most common methods should be Classloader.loadclass(string name, Boolean resolve), and class.forname(string name), Both of these methods are used to load class, but there are some differences in function.

The LoadClass method is protected, the user can directly invoke the LoadClass (String name), resolve defaults to False, that is, the class is not resolved, the LoadClass does not parse the class when loading the class, Therefore, the class is not initialized. forname (String name) is equivalent to Class.forName ("ClassName", True,callclass.class.getclassloader), which interprets and initializes the class when it is loaded. .

There are three steps to load, connect, initialize the JVM when it loads a class.

  Loading:

1. The binary data stream of the corresponding class is generated by the full name of the class. (Note that if the corresponding class file is not found, an error is thrown only when the class is actually used.) )

2. Analyze and convert these binary data streams to a method area (the JVM's schema: Method area, heap, stack, local method stack, PC register) specific data structures (these data structures are implementation-related, different JVMs have different implementations). Some tests are handled here, such as the validation of the magic number of the class file, checking if the file is too long or too short, and determining if there is a parent class (except for the Obecjt Class).

3. Create a Java.lang.Class instance of the corresponding class (note that with the corresponding class instance, it does not mean that the class has completed the load chain link!) )。

  Connection:

  1. Verify that the class conforms to the rules, such as checking that the final class is not inherited, checking the correctness of static variables, and so on;

2. Prepare to allocate space for member variables of the class. Although there are initial values, they are not initialized at this time (because no Java code is executed here). As follows: All primitive types have values of 0. such as float:0f, int:0, boolean:0 (note that the boolean underlying implementation mostly uses int), and the reference type is null.

3. Explanation, locate the direct reference for the symbol reference of the class, interface, method, member variable. This step is optional and can be done when the symbol reference is first used, the so-called delay resolution (late resolution). However, for the user, this step is always deferred parsing, even if the runtime will execute early resolution, but the program will not show the first error when the error is thrown, but the corresponding class will be the first time the active use of the error thrown!

  Initialization

1. If the base class is not initialized, initialize the base class.

2. There is a class constructor, then the class constructor is executed.

Description: Characters in Java exist only in one form, which is Unicode.

Second, type conversion

  Simple data types are from low to Advanced order (Byte,short,char)-int-long-float-double;

Low to advanced can be automatically converted, advanced to low level, and between peers need to be cast.

1. The default 1.0 represents a double type, and if you need to represent float, write it as 1.0f.

2. When char x= ' x ';  int i = 10; The formula is false? The value of the I:X output is 120, because both are variables, which upgrade the lower level to the advanced representation; false?10:x, the output of x is still x because the Java programming specification mentions that when two expressions have one constant expression (10) and the other is the T type, When a constant expression can be represented by T, the output is a T-type.

3. Three mesh operation is right-associative.

Third, abnormal

 Both error and exception inherit from the Throwable class.

Error is not controllable and is often used to indicate a system error or an error in the underlying resource.

Exception can be controlled (checked) or uncontrollable (unchecked) and should be handled at the application layer, throw represents an exception, and throws is declared outside the method body that may throw some exceptions.

The program must catch or declare all non-runtime exceptions.

  

Java Common knowledge Collation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.