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

查看:293
本文介绍了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 声明(不是语句)本质上是源代码级别的简写启用程序:它允许您引用类型或 static 使用单个标识符的成员(例如 List min )而不是完全限定名称(例如 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.


导入声明允许 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天全站免登陆