This article is transferred from www.35java.com
Java has two package import mechanisms, which are summarized as follows:
Single-type import (single-type-import), such as import java. Io. file;
Type-import-on-demand, for example, import java. Io .*;
There are different opinions about the two import types. Here we will analyze the general working principles of the two import types for your reference.
Single-type import is easy to understand. Only one public class or interface is imported. For on-demand type import, some people misunderstand it as importing all the classes under a package. Otherwise, they will know the name and only import as needed. That is to say, it is not to import the entire package, instead, only the classes required for the current class are imported.
In this case, can we safely use the on-demand type import? Neither. Because of Single-type import and on-demand import, class files are locatedAlgorithmIs different. The Java compiler locates the classes to be imported from the bootstrap directory, extension directory, and user class path. These directories only provide the top-level directory of the class. The class file location method of the compiler can be roughly understood as the following formula:
Top-level path name \ package name \ file name. Class = absolute path
Single-type import is very easy. Because the package and file name have been determined, you can find and locate the package at one time.
On-demand type import is complicated. The compiler will sort and combine the package name and file name, and then search and locate all possibilities for class files. For example:
Package com; Import java. Io .*; Import java. util .*; |
when the file class is used in your class file, the following file class may appear:
the file \ file class belongs to the unknown package, that is, the file class does not have a package statement, the compiler first searches for the unknown package
COM. the file \ file class belongs to the current package
JAVA. lang. the file \ compiler will automatically import Java. lang Package
JAVA. io. file
JAVA. util. file
note that the compiler finds Java. io. the next step is not to stop searching for the file class, but to check all possibilities to determine whether there is a class import conflict. Suppose there are three top-level paths at this time, the compiler will perform 3*5 = 15 searches.
Note: If the compiler finds two classes with the same name after the search is complete, an error is returned. Delete the class you don't need and then compile it.
after understanding the above principles, we can draw the following conclusion: on-demand type import will never reduce the Java Code execution efficiency, but will affect the compilation speed of Java code.
View the Source Code of JDK. Sun's software engineers generally do not use on-demand import. Because single-type import has at least two advantages:
1. Increase Compilation speed.
2. Avoid name conflicts. (For example, when you import Java. AWT. *; import Java. util. * After using list, the compiler will produce a compilation error.
of course, using single-type import will make your Import Statement look very long.