Second, Java object-oriented (7) _ Packaging Ideas--import Keywords

Source: Internet
Author: User

2018-04-30

Import Keyword

The import declaration can be divided into two:
1> single-type import (Single-type-import), only one class can be imported

   Syntax format: The fully qualified name of the import class;

Example: Import java.util.ArrayList;
2> on-demand type import (Type-import-on-demand), which means that all the current folders under the package are imported used to the class

Syntax format: Import package name. The name of the word package. *;
Example: Import java.util.*;

In the Eclipse tool, even with * import, it is converted to n import statements when formatting code

To import classes and interfaces for any of the public in a package in two ways (only public classes and interfaces can be imported)

* Import claims import only types without importing child packages, which is why they are called single-type and on-demand types for import claims.

* The simplified name of the imported class or interface has a compilation unit scope. This means that the type name can be used anywhere in the compilation unit where the import statement resides. This does not mean that you can use the simple name of all members of the type, but only the name of the type itself.
For example, the public class in the Java.lang package is imported automatically, including the math and system classes. However, you cannot use the simple name PI () and GC () and must use Math.PI () and System.GC (). What you don't need to type is Java.lang.Math.PI () and JAVA.LANG.SYSTEM.GC ().

The programmer sometimes imports the current package or the Java.lang package, which is not required because the members of the current package are themselves within scope, the Java.lang package is automatically imported . the Java compiler ignores these redundant import declarations (redundant import declarations). Even if it's like this
Import java.util.ArrayList;
Import java.util.*;
Multiple imports, can also be compiled through. The compiler ignores redundant import claims.

The compiler will automatically go to Java.liang to find the classes used, such as String,system, so they don't need to be imported. (Inference: You need to import all the packages except Java.liang.) )

-----------------------------------------------------------------

Does using on-demand import claims reduce the efficiency of Java code execution? Absolutely not!
The Java compiler produces a class file that contains only the symbolic references of the class or interface to which the compilation unit is actually used.

-----------------------------------------------------------------

Does this mean that you can always use an on-demand import declaration? Yes, no!
It is useful to use an on-demand import declaration in an informal development like demo.

---------------------------------------------------------------------------------------------------------
However, there are four reasons why you can abandon such a statement:
1> compile speed: In a large project, they can greatly affect the speed of compilation. However, the use of small projects is negligible at compile time.
2> naming conflicts: The answer to the problem of avoiding naming conflicts is to use the full name. And on-demand import is exactly the negation of using the import declaration.
3> explains the problem: the use of full names is self-explanatory. After all, high-level language code is for people to see.
4> Nameless Package Problem: If there is no package declaration at the top of the compilation unit, the Java compiler prefers to search for a type from the nameless package and then the on-demand type declaration. If there is a naming conflict, it can cause problems. 、


Sun's engineers generally do not use the on-demand type to import claims. This you can find in their code:
import declarations in the Java.util.Properties class:
import java.io.IOException;
import Java.io.printStream;
import Java.io.printWrite;
import Java.io.InputStream;
import Java.io.BufferedReader;
import Java.io.BufferedWriter;
import java.util.Hashtable;

You can see the interesting is that she even java.util.Hashtable import, this is in the current package Ah!

Second, Java object-oriented (7) _ Packaging Ideas--import Keywords

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.