如果 Java 没有预处理器,那么“导入”是什么?
这篇文章说 2.2 中不再有类型定义、定义或预处理器.1.在 C++ 中,include
是预处理器的一部分。什么是导入
?
This article says No More Typedefs, Defines, or Preprocessor in 2.2.1 . In C++ the include
is part of the preprocessor. What is the import
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管名称不会“导入”任何内容,但导入只是让您可以在没有完全限定名称的情况下调用类。
澄清一下,如果我执行
import java.util.ArrayList;
,现在我可以将ArrayList
类引用为ArrayList
。如果我不这样做,我仍然可以使用该类,只需将其命名为java.util.ArrayList
。如果你用
*
导入整个包,最糟糕的情况就是发生名称冲突,因此,你必须使用全名来引用 Java 类,但事实并非如此。运行时不要使用更多内存。java.lang
中的类会自动“导入”。Java 1.5 引入了静态导入,使程序员能够引用导入的静态成员,就像它们是在使用它们的类中声明的一样。应谨慎使用它们。可接受的用途是导入 JUnit Assert 方法。例如,使用传统导入:
使用静态导入:
Import despite the name doesn't "import" anything, it just let you call the classes without the fully qualified name.
To clarify, if I do an
import java.util.ArrayList;
, now I can refer toArrayList
class as justArrayList
. If I don't do it, I can still use the class, I just have to call itjava.util.ArrayList
.If you import whole packages with
*
, the worst thing it can happen is that there is a name clash, thus, you've to use the full name to refer to a Java class, but it doesn't use more memory at runtime.Classes in
java.lang
are automatically "imported".Java 1.5 introduced static imports, which enables programmers to refer to imported static members as if they were declared in the class that uses them. They should be used sparingly. An acceptable use is for importing JUnit Assert methods. For instance, with a traditional import:
With static import:
导入允许您使用非限定类名。例如,通过
import java.util.ArrayList
,您可以在代码中使用非限定类型名称ArrayList
。如果没有 import 语句,您必须始终使用完全限定名称:java.util.ArrayList
。还有静态导入,它将静态类元素引入编译单元的名称空间。
Import allows you to use an unqualified class name. For instance, with
import java.util.ArrayList
you can use an unqualified type nameArrayList
in your code. Without the import statement, you would have to always use the fully qualified name:java.util.ArrayList
.There's also static import, which brings static class elements into the name space of the compilation unit.
import
使包名称在使用它的文件中已知。它根本无法与C 的#inlinclude
相提并论。import
makes package names known within the file where it is used. It is not at all comparable to C's#inlclude
.由于严格的源代码命名约定,Java 编译器只需从包和类的完全限定名称即可轻松找到相应的源文件或类文件。通过完全限定名称,我的意思是指定完整的包和类,例如,
这种冗长编码风格的替代方法是使用 import 语句。
对于理解别人的代码也有很大的帮助
Because of the rigid source code naming convention, the Java compiler can easily find the corresponding source or class files just from the fully qualified name of a package and class. By fully qualified name I mean specifying the full package and class e.g.
The alternative to this long-winded style of coding, is to use import statements.
Also it is a great help to understanding someone else’s code
有一种方法可以告诉编译器您正在使用另一个包中的某个类吗?
编辑:规范链接
A way to tell the compiler you are using some class from another package?
EDIT: link to the spec