Java 文件中 import 语句的含义 [英] Meaning of the import statement in a Java file

查看:27
本文介绍了Java 文件中 import 语句的含义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能清楚地向我解释当我们在 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)本质上是简写的启动器在源代码级别:它允许您使用单个标识符(例如 Listmin)来引用类型或 static 成员作为与完全限定名称相反(例如 java.util.ListMath.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 appropriate import declaration, the only way to refer to a type declared in another package, or a static 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.

参考资料

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆