1, Java knowledge base

 

1, on the. Java file
It states: The first line package name; which indicates that the .java file belongs to a package.
A .java file can have multiple classes, but only one public class, and the public must be the same class with .java file names (case sensitive).
 
2, package (packages)
The concept: Package Penalty for express package, similar to folders.
· · · Note: If a .java file declares the package, when using javac compiler to be used with a package that is compiled:. Javac -d name.java, or in the implementation .class file error (could not find or could not be loaded The main class).
··· package naming convention for the domain name upside down, for example: com.company.department.group.project.module; stepwise refinement.
 
3, CLASSPATH and JAVA_HOME role
classpath is the storage location of the java compiler generates .class files by default. That generated .class files in the current directory, java1.6 no configuration.
java_home is java installation root directory.
 
4, class loading
· · · Concept:
When JVM first time a class, you need to find the class .class file through the CLASSPATH, and the .class file description of the class is loaded into memory, save (such as: the package name, class name, parent name, property A method, constructor, etc.).
When ??? load
    *  Passive load: creating objects, create sub-class object, access a static property, call the static method.
    ·  Active load: Class.forName ( "fully qualified name"); // similar reflection.
· · · Note:
    ·  Use "parents delegate mechanism" when the class is loaded, first by starting and standard extension is not on view in class java.lang package, if not, then the system class loader to load user-defined classes. This prevents a hacker to modify the type of package lang.
    ·  Can isolate the classes loaded by the class loader, such as when a Tomcat can deploy multiple projects, the project is to isolate different classes loaded by the class loader.
 
5, block
??? static block of code: when the class loader is performed (and only once), can be used to initialize static properties. // stataic {}
??? dynamic block: When the execution object is created, similar to the configuration method.
··· process code block: used to isolate the scope of variables.
 
6, when the steps of creating an object
??? class loader (if not already loaded class):
    Initialize static properties;
    Performing static code block;
????? object initialization (constructor is executed):
    Initializes instance attributes;
    Perform dynamic code block;
    Executing the content of the construction method;
 
Precautions
    ·  Constructor can not be called recursively.
    · IDEA in the jar to add Modules, in fact, in the implementation of the java command to add the path to the jar package -classpath parameter. -classpath JVM is to start the program directory of all the .class file is located, and then find the entrance main method to begin.
    ·  Runtime.getRuntime (). AddShutdownHook (new new the Thread) can add a hook, when the JVM exits, will perform this thread.

 

Guess you like

Origin www.cnblogs.com/shendeng23/p/12417035.html