Some people write Java for a while, but do not know much about Java package and import? Why? The import in the program. java file will allow the compiler to write the imported program to the compiled. class file, or recognize it? Import and C/C ++
Include is similar. In fact, this is a wrong idea.
Let's take a look at the usefulness of Java package. In fact, the package name is like our surname, and the class name is like our name. There are many package names, such as the surname. For example, java. lang. String is the name of java. lang? String type; java. io. InputStream is the surname of java. io. What is the name? InputStream category.
The reason why Java uses the package mechanism is also obvious. Just like our name, many students with the same name may appear in the same school, if you do not take the last name, the school is processing the student information or the names of the students.
Confusions. In the same way, the number of Java classes in the world is probably more than that in Taiwan, and it is still growing. If the category does not use the suite name, when different classes with the same name are used,
Yes? It is a great headache. Fortunately, we can get the Java suite name by ourselves. We don't have much to choose from the person's surname (so there are many people with the same name and surname). If we get the suite name according to Sun's specifications, in theory, the name of the suite won't be the same for different people (see "naming conventions"
), So there will be no name? Sudden. But the question is, why? Is it accurate? Take? Product Introduction 5 a region? Why? Braised oysters
It takes a lot of time, for example:
The code is as follows: |
Copy code |
Java. io. InputStream is = java. lang. System. in; Java. io. InputStreamReader isr = new java. io. InputStreamReader (is ); Java. io. BufferedReader br = new java. io. BufferedReader (isr );
|
In zz. java, because all classes in package a are introduced, the usage is like in the same package (of course, the access permission must be satisfied first, which is assumed to be accessible ).
In a program, you can introduce all or several classes of the package. To introduce all classes, you can use the wildcard "*", for example:
The code is as follows: |
Copy code |
Import java. lang .*; |
It is not beautiful and troublesome. So Sun came up with a solution: import.
This import indicates the classes that will be used in the program at the beginning of the program.
That is, it only calls the name, not the surname. First, write at the beginning of the file:
The code is as follows: |
Copy code |
Import java. lang. System; Import java. io. InputStream; Import java. io. InputStreamReader; Import java. io. BufferedReader; |
These lines describe the categories of these four names. They are called only by their names in the program.
System refers to java. lang. System, and InputStream refers
Java. io. InputStream, and so on. The original program becomes:
The code is as follows: |
Copy code |
InputStream = System. in; InputStreamReader isr = new InputStreamReader (is ); BufferedReader br = new BufferedReader (isr ); |
Is it refreshing? If these categories are frequently used, you can better understand them.
Import benefits. But this is not enough, because? What is the milk? Therefore, xianxiosi
Playing too many import operations is also a waste of time, so Sun provides another method:
The code is as follows: |
Copy code |
Import java. lang .*; Import java. io .*; |
That is, wait for the class without name mentioned in the program, either java. lang or surname.
Java. io, if the two? There is a category with the same name, and unfortunately you only use the name to name this
Then the compiler will complain with you, because? Why? Why? Abuse of documents and annoyance
The category of the surname. Can I be more lazy? Just write:
The code is as follows: |
Copy code |
Import java .*; |
? V Shi tells us that people can be lazy, but not too lazy. Because? ├
Java. io instead of java. Like a person named "Zhuge", shouldn't you like him? Taobao.com
Sir.
? N I initially said that import is different from # include? Because? Import functions
So far? Stem ?? The file content is loaded in the same way as # include. Import only
The compiler can help you type, so that the compiler adds a class without a surname to the class without a surname, and does not include other files.
Type code. If you want to practice typing, do not use import.
You can call it with all its names (just like in the first example).
What is import? Two.
In addition, although people cannot be too lazy, Sun is still a little lazy to help us. Because?
Java. lang is too often and too often used, and almost no program needs to use it,
So no matter whether you write the import java. lang;, the compiler will automatically help you complete it.
It means that the compiler will automatically go to java. lang as long as it sees a class without a surname? Face search,
Check whether this category belongs to this suite. So we don't need to do anything special.
Import java. lang.
Instance
The code is as follows: |
Copy code |
Package xx. bb. aa; |
All classes in this. java compilation unit are placed in the package xx. bb. aa. The. java file must be placed in the aa directory under the bb directory under the xx directory. If A. java file does not have any package statements, all classes in this. java file are under the package "/", which is also called the default package. It can be seen that the class of the first hello world program written in any java textbook is in defaultpackage. With the package statement, the situation is more complicated. This compilation unit. java must be placed under the Directory corresponding to the package name. The generated class file must be placed under the corresponding directory structure to operate normally.
For example:
The code is as follows: |
Copy code |
/* A. java */ Package aaa. bbb. ccc; Public class { B B = new B (); } /* B. java */ Package aaa. bbb. ccc; Public class B {} |
During compilation, what is the limit parameter? I write it in the format of package + file name,
The code is as follows: |
Copy code |
Javac aaa. bbb. ccc. A. java |
Pretty? Unfortunately, it does not work. You must use a valid path name:
The code is as follows: |
Copy code |
Javac aaa/bbb/ccc/A. java |
However, you find that the generated class has lost the directory structure and appears directly in the current directory ......
The best way is
The code is as follows: |
Copy code |
Javac-d bin aaa/bbb/ccc/A. java |
In this way, you will see the complete directory structure and properly placed class files under the bin directory of the current directory.
What package and classpath have to say
For java, all required programs and resources must be organized and read in the form of a package.
................................
The import declaration can be divided into two parts:
1> single-type import)
Example:
The code is as follows: |
Copy code |
Import java. util. ArrayList; |
Java. util. ArrayList.
If you use import java. util. ArrayList;
You can write ArrayList in the code to save trouble. The wildcard * can be used for import. * represents all classes in a package, excluding subdirectories.
The code is as follows: |
Copy code |
Import java. awt .* |
Not equal
The code is as follows: |
Copy code |
Import java. awt .* Import java. awt. event .* |
If you want to short the classes under java. awt. event and java. awt, you cannot be lazy. Both must be imported.
2> type-import-on-demand)
Example:
The code is as follows: |
Copy code |
Import java. util .*; |
Import any public class and interface in the package in these two ways (only the public class and interface can be imported)
* The import declaration only imports data types without importing sub-packages. This is why they are called single-type imports and on-demand import declarations.
* The simple name of the imported class or interface has the scope of the compilation unit. this indicates that this type of short name can be used anywhere in the compilation unit where the import statement is located. this does not mean that you can use the names of all members of the type, but only the names of the members of the type.
Example: java. the public classes in the lang package are automatically imported, including Math and System classes. however, you must use Math instead of PI () and gc. PI () and System. gc (). you do not need to enter java. lang. math. PI () and java. lang. system. gc ().
Programmers sometimes import the current package or java. lang package, this is not required, because the current package members are in the scope, and java. the lang package is automatically imported. the java compiler ignores these redundant import declarations (redundant import declarations ). even if
The code is as follows: |
Copy code |
Import java. util. ArrayList; Import java. util .*;
|
Multiple imports can also be compiled. The compiler ignores redundant import declarations.
Does the use of on-demand import declarations reduce Java code execution efficiency? Absolutely not!
The class file generated by the Java compiler only contains the symbolic references of the classes or interfaces actually used by the compilation unit.
Does this mean that you can always use the on-demand import declaration? Yes, no!
It is useful to use on-demand import declarations in informal development similar to Demo.
However, there are four reasons for you to give up the statement:
1> compilation speed: in a large project, they will greatly affect the compilation speed, but it is negligible to use it in small projects.
2> name conflict: the answer to the problem of avoiding name conflicts is to use the full name. On-demand import is just a rejection of the original intention of using the import statement.
3> Note: The full name is self-speaking. After all, the code in the advanced language is for people to see.
4> unknown package: if there is no package declaration at the top of the compilation unit, the Java compiler will first search for a type from the unknown package, and then the on-demand type declaration. if a name conflict exists, a problem may occur.
Sun engineers generally do not use the on-demand import declaration, which can be found in their code:
Import declaration in the java. util. Properties class:
The code is as follows: |
Copy code |
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 that it is interesting that she even imports java. util. Hashtable, which is in the current package!
In Java, to use the package features, you can use the import statement to tell the compiler where the class to be used is located. In fact, the package name is also part of the class name. For example, if the abc. FinanceDept package contains the Employee class, the class can be called abc. FinanceDept. Employee. If the import statement is used and the class is used again, the package name can be omitted and only the Employee ID is used to specify the class.
The format of the introduced statement is as follows:
The code is as follows: |
Copy code |
Import pkgl [. pkg2 [. pkg3…]. (Class name I g -);
Example 5-18 Assume that there is a package a, and two classes xx and YY are defined in a file in a. The format is as follows: Package; C | ass xx { } Class YY { } When the class in a is used in the file ZZ. java of another Package B, the statement format is as follows: // Zz. java Package B; Import .*; Class zz extends XX { Yy y; } |
In zz. java, because all classes in package a are introduced, the usage is like in the same package (of course, the access permission must be satisfied first, which is assumed to be accessible ).
In a program, you can introduce all or several classes of the package. To introduce all classes, you can use the wildcard "*", for example:
The code is as follows: |
Copy code |
Import java. lang .*; |
When the entire package is introduced, you can easily access every class in the package. In this way, the statement is easy to write, but it occupies too much memory space, and the download time of the code will be extended. Beginners can introduce the entire package. However, we recommend that you introduce the classes that are actually used after learning about the basic content of the package, so as not to waste resources as much as possible.