Java Import)

Source: Internet
Author: User

 

Reprinted from: http://java.chinaitlab.com/base/726310.html

 

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. The algorithm for locating class files is different for single-type import and on-demand import.

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 file class may appear as follows:

1. The file // File class is an unknown package. That is to say, the file class does not have a package statement. The compiler will first search for an unknown package.

2. the com. File // File class belongs to the current package.

3. java. Lang. File // The Compiler automatically imports the java. lang package.

4. java. Io. File

5. java. util. File

Note that after the compiler finds the java. Io. File class, it does not stop looking for the next step. Instead, it needs to find 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.

After understanding the above principles, we can draw the conclusion that on-demand type import will never reduce the execution efficiency of Java code, but will affect the compilation speed of Java code.

By checking the JDK source code, sun's software engineers generally do not use on-demand type imports. Because single-type import has at least two advantages:

1. Improve compilation speed.

2. avoid naming conflicts. (For example, when you import java. AWT. *; import java. util. *, the compiler will produce a compilation error when you use list)

Of course, using single-type import will make your Import Statement look very long.

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.