The essence of namespace, use and requie of java import, package and php

First of all, the working principle of java is, first go to the java environment variable classpath or your current compilation directory to find if there is any class you need

Import and package are not followed by the real path of the file. In fact, they do not need to organize folders by name. You can compile and run them normally if you put them all in one directory. It's organized by file path just for human convenience.
Its real purpose is to avoid class conflicts.

The package name is like our last name, and the class name is like our first name.
Therefore, import is different from #include of c because the function of import ends here. It is not like #include, which will include the contents of other files load in. import just makes the compiler add the last name to the categories without last name when compiling this java file, and will not write other file programs into it.

So what are the benefits of using it?

  • Avoid conflicts of class attributes. With so many java classes in the world, there will always be conflicts, which require package name identification
  • Avoid writing lengthy package names every time. Java can also directly write the full name of the class with the package name without importing the class, for example, you can directly write java.lang.System.in but it is too troublesome to write like this every time, so after you import java.lang.System, You can just write System.in in the code

Is it possible to directly introduce all classes import java.*?
History tells us that this will not work. Because those categories are surname http://java.io not surname java. Just like a person with the surname "Zhuge" probably wouldn't like you calling him Mr. "Zhu". In this way, only the classes under the java package will be declared, and no classes in subpackages will be declared.

Will using the mode of importing * on demand reduce efficiency?
Conclusion: It will not reduce the efficiency of code operation but will affect the efficiency of compilation, because in essence, it is to replace the abbreviation in the code you write with the full name. So it will affect the speed of compilation

The problem it creates is as follows

  1. Compilation speed: In a large project, they will greatly affect the compilation speed, but in a small project, the compilation time is negligible.
  2. Naming conflicts: The answer to the problem of avoiding naming conflicts is to use full names. On-demand import is precisely the negation of the original intention of using the import statement.
  3. Explanation of the problem: After all, the code of the high-level language is for people to see, and the specific type used cannot be seen when imported on demand.
  4. Unnamed package problem, if there is no package declaration at the top of the compilation unit, the Ja compiler will first search for a type from the unnamed package, and then the on-demand type declaration. Problems can arise if there are naming conflicts.

And php requires require to be introduced before it can be used. It is different from the mechanism of namespace and use

PS
Programmers sometimes import the current package or the java.lang package, which is unnecessary because the members of the current package are themselves in scope, and the java.lang package is automatically imported. The java compiler will ignore these redundant import declarations (redundant import declarations). even like this

import java.util.ArrayList;

import java.util.*;

Multiple imports can also be compiled. The compiler ignores redundant import declarations.

Guess you like

Origin blog.csdn.net/S_ZaiJiangHu/article/details/131501603