Java 文件中 import 语句的含义 [英] Meaning of the import statement in a Java file
问题描述
谁能清楚地向我解释当我们在 Java 文件中使用 import 语句时到底发生了什么?如果我们添加越来越多的 java 类,它会增加文件的大小吗?为什么我们不使用类加载器呢?进口有什么限制?
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?
推荐答案
import
declarations(不是 statements)本质上是简写的启动器在源代码级别:它允许您使用单个标识符(例如 List
、min
)来引用类型或 static
成员作为与完全限定名称相反(例如 java.util.List
、Math.min
).
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
声明部分是源代码的编译时元素,在运行时不存在.在 JVM 字节码中,类型名称始终是完全限定的,除非您使用编写得不好的编译器,否则二进制文件应该只包含实际使用的类型名称.
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
功能完全无关.
Class loaders are used for an entirely different concept, and has nothing to do with import
feature at all.
import 声明允许一个 static
成员或一个命名类型被一个由单个标识符组成的简单名称引用.如果不使用适当的 import
声明,引用在另一个包中声明的类型或另一个类型的 static
成员的唯一方法是使用完全限定的名字.
An import declaration allows 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.
参考资料
- JLS 7.5.1 Single-Type-进口申报
- JLS 7.5.2 类型-导入-按需声明
- JLS 7.5.3 单一静态导入声明
- JLS 7.5.4 静态导入-按需声明
- 什么是
import
? -这是一个声明,而不是一个声明 - What is an
import
called? - it's a declaration, not a statement - 导入包.* vs 导入包.SpecificType
- 为什么使用野生带有 Java 导入语句的卡不好吗?
- 有什么区别
import java.util.*;
和import java.util.Date;
? - What does the
static
modifier afterimport
mean? - What is a good use case for static import of methods?
- Should I use static import?
- Does importing of packages change visibility of classes? - ABSOLUTELY NOT!
- Do multiple import statements in a program affect performance? - NOPE!
- Any reason to clean up unused imports in Java, other than reducing clutter?
关于import
的语法作用:
按需与单一类型:
关于import static
:
与性能相关的问题:
这篇关于Java 文件中 import 语句的含义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!