Java 文件中 import 语句的含义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2903166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Meaning of the import statement in a Java file
提问by Pedantic
Can any one clearly explain to me what exactly happens when we use the import statement in Java files? Does it increase the size of the file if we add more and more java classes? Why don't we use class loader for the same? And what are the restrictions for importing?
谁能清楚地向我解释当我们在 Java 文件中使用 import 语句时到底发生了什么?如果我们添加越来越多的java类,它会增加文件的大小吗?为什么我们不使用类加载器呢?进口有什么限制?
回答by Snake
import
statements say to the compiler: if you have a function that cannot be found in this class look in the list of imports.
import
语句对编译器说:如果您有一个在此类中找不到的函数,请查看导入列表。
This way you can refer to functions in other packages without having to copy the definition (like C(++) .h files) to your own package.
这样您就可以引用其他包中的函数,而无需将定义(如 C(++) .h 文件)复制到您自己的包中。
回答by Zaki
Packages consist of classes, classes in a package consist of methods, variables etc etc. A class has a full name which comprises of the package name and the class name. If you need to use a class in your code,you need to give the compiler the full name of the class.So, you use an import statement OR you can type the fully qualified name every place you use that class in your code.
包由类组成,包中的类由方法、变量等组成。类的全名由包名和类名组成。如果您需要在代码中使用类,则需要为编译器提供类的全名。因此,您可以使用导入语句,或者您可以在代码中使用该类的每个地方键入完全限定名称。
For example, if you need an AraryList
in your code, you use the import statement import java.util.ArrayList;
instead of typing the fully qualified class name every place you need an Arraylist.
例如,如果您AraryList
的代码中需要 ,则使用 import 语句import java.util.ArrayList;
而不是在需要 Arraylist 的每个地方键入完全限定的类名。
For more detailed info, see JLS.
有关更多详细信息,请参阅JLS。
回答by Hardcoded
The imports in java are only hints for the compiler. It doesn't affect the size of the binary class file at all. You can either use an imports once or write the full name of the Class every time you use it.
java 中的导入只是对编译器的提示。它根本不影响二进制类文件的大小。您可以使用一次导入,也可以在每次使用时写下类的全名。
Imports are just a concession to readability and the laziness of the developer.
导入只是对可读性和开发人员懒惰的让步。
回答by polygenelubricants
import
declarations(not statements) are essentially short-hand enabler at the source code level: it allows you to refer to a type or a static
member using a single identifier (e.g. List
, min
) as opposed to the fully qualified name (e.g. java.util.List
, Math.min
).
import
声明(不是语句)本质上是源代码级别的简写启用器:它允许您static
使用单个标识符(例如List
, min
)而不是完全限定名称(例如java.util.List
, Math.min
)来引用类型或成员。
import
declaration section is a compile-time element of the source codes, and has no presence at run-time. In JVM bytecodes, type names are always fully qualified, and unless you're using a poorly written compiler, the binary should only contain names for types that are actually being used.
import
声明部分是源代码的编译时元素,在运行时不存在。在 JVM 字节码中,类型名称始终是完全限定的,除非您使用编写得不好的编译器,否则二进制文件应该只包含实际使用的类型的名称。
Class loaders are used for an entirely different concept, and has nothing to do with import
feature at all.
类加载器用于完全不同的概念,与import
功能完全无关。
JLS 7.5 Import Declarations
JLS 7.5 导入声明
An import declarationallows a
static
member or a named type to be referred to by a simple name that consists of a single identifier. Without the use of an appropriateimport
declaration, the only way to refer to a type declared in another package, or astatic
member of another type, is to use a fully qualified name.A single-type-import declaration imports a single named type, by mentioning its canonical name.
A type-import-on-demand declaration imports all the accessible types of a named type or package as needed. It is a compile time error to import a type from the unnamed package.
A single static import declaration imports all accessible static members with a given name from a type, by giving its canonical name.
A static-import-on-demand declaration imports all accessible static members of a named type as needed.
一个进口报关允许
static
部件或命名类型通过由单个标识符的简单名称来引用。如果不使用适当的import
声明,引用在另一个包中声明的类型或static
另一个类型的成员的唯一方法是使用完全限定名称。单一类型导入声明通过提及其规范名称来导入单个命名类型。
按需类型导入声明根据需要导入命名类型或包的所有可访问类型。从未命名的包中导入类型是编译时错误。
单个静态导入声明通过给出其规范名称,从类型中导入具有给定名称的所有可访问静态成员。
按需静态导入声明根据需要导入命名类型的所有可访问静态成员。
References
参考
- JLS 7.5.1 Single-Type-Import Declaration
- JLS 7.5.2 Type-Import-on-Demand Declaration
- JLS 7.5.3 Single Static Import Declaration
- JLS 7.5.4 Static-Import-on-Demand Declaration
See also
也可以看看
Various import
related questions
各种import
相关问题
On the grammatical role of import
:
关于 的语法作用import
:
- What is an
import
called?- it's a declaration, not a statement
- 什么
import
叫叫?- 这是声明,而不是声明
On on-demand vs single-type:
按需与单一类型:
- Import package.* vs import package.SpecificType
- Why is using a wild card with a Java import statement bad?
- What's the difference between
import java.util.*;
andimport java.util.Date;
?
- 导入 package.* 与导入 package.SpecificType
- 为什么在 Java 导入语句中使用通配符不好?
import java.util.*;
和 和有import java.util.Date;
什么区别?
On import static
:
在import static
:
- What does the
static
modifier afterimport
mean? - What is a good use case for static import of methods?
- Should I use static import?
Performance-related issues:
性能相关问题:
回答by polygenelubricants
The import statement in Java allows to refer to classes which are declared in other packages to be accessed without referring to the full package name. You do not need any import statement if you are willing to always refer to java.util.List by its full name, and so on for all other classes. But if you want to refer to it as List, you need to import it, so that the compiler knows which List you are referring to.
Java 中的 import 语句允许引用在其他要访问的包中声明的类,而无需引用完整的包名称。如果您愿意始终按全名引用 java.util.List,则不需要任何 import 语句,对于所有其他类,依此类推。但是如果你想引用它为List,你需要导入它,这样编译器就知道你引用的是哪个List。
Classes from the java.lang package are automatically imported, so you do not need to explicitly do this, to refer to String, for example.
java.lang 包中的类会自动导入,因此您无需显式执行此操作,例如引用 String。
Read more: http://wiki.answers.com/Q/Why_import_statement_is_needed_in_Java_program#ixzz1zDh2ZBhE
阅读更多:http: //wiki.answers.com/Q/Why_import_statement_is_needed_in_Java_program#ixzz1zDh2ZBhE