Import mechanism in Java (Specify the difference between import and import *)

Source: Internet
Author: User

There are two types of package import mechanisms in Java, summarized as follows:

Single-type import (Single-type-import), such as import java.io.File;

On-demand type import (Type-import-on-demand), such as import java.io.*;

Here is an analysis of the two types of import of the approximate working principle for everyone to refer to.

Single-type import better understand, just import a public class or interface.

For on-demand type import (import java.io.*; ), someone misunderstood to import all the classes under a package, in fact, the name will know that he will only import on demand, that is, it does not import the entire package, but only to import the current class needs to use the class.

So is it safe to use the on-demand type to import? Non-also, non-also.

Because single-type import and on-demand type import have different positioning algorithms for class files. The Java compiler locates the classes that need to be imported from the startup directory (bootstrap), the extended directory (extension), and the user class path, which simply gives the top-level directory of the class. The compiler's class file positioning method can be broadly understood as the following formula:

Top-level path name \ package name \ file name. class = Absolute path

For single-type import it is simple, because Baoming and filenames are already determined, so you can find the location at once.

For on-demand type import is more complex, the compiler arranges package names and filenames, and then locates all the possibilities for class file lookups. For example:

Package com;

Import java.io.*;

Import java.util.*;

When you use the file class in your class files, the file class may appear as follows

The file \ \ File class belongs to the nameless package, which means that the file class does not have a package statement, and the compiler first searches for nameless packages

Com. The file \ \ File class belongs to the current package

Java.lang.File \ \ compiler will import Java.lang package automatically

Java.io.File

Java.util.File

It is important to note that after the compiler finds the Java.io.File class, it does not stop looking for the next step, but all the possibilities are looked up to determine if there is a class import conflict. Assuming that there are three top-level paths at this point, the compiler will perform a 3*5=15 lookup.

Note: If the compiler discovers two classes with the same name after the lookup is complete, an error will be made. To delete the class that you don't use, and then compile it.

Knowing the above, we can conclude that the on-demand type import will never degrade the execution efficiency of Java code, but it will affect the compilation speed of Java code.

View the source code of the JDK you know that Sun's software engineers don't typically use on-demand types for import. Because using single-type imports has at least the following two benefits:

1. Improve compilation speed.

2. Avoid naming conflicts. (Example: When you import Java.awt.*;import java.util.*, the compiler will compile errors when using list)

Of course, using a single-type import will look very long with your import statement.

Import mechanism in Java (Specify the difference between import and import *)

Related Article

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.