gpt4 book ai didi

java - Java文件中import语句的含义

转载 作者:IT老高 更新时间:2023-10-28 21:04:13 27 4
gpt4 key购买 nike

谁能清楚地向我解释当我们在 Java 文件中使用 import 语句时到底发生了什么?如果我们添加越来越多的 java 类,它会增加文件的大小吗?为什么我们不使用类加载器呢?进口有什么限制?

最佳答案

import declarations(不是 statements)本质上是源代码级别的简写使能器:它允许您引用一个类型或使用单个标识符(例如 Listmin)而不是完全限定名称(例如 java.util)的 static 成员.List, Math.min).

import 声明部分是源代码的编译时元素,在运行时不存在。在 JVM 字节码中,类型名称始终是完全限定的,除非您使用编写不佳的编译器,否则二进制文件应该只包含实际使用的类型的名称。

类加载器用于完全不同的概念,与 import 功能完全无关。

JLS 7.5 Import Declarations

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.

引用文献

另见


各种import相关问题

import的语法作用:

按需与单一类型:

关于导入静态:

性能相关问题:

关于java - Java文件中import语句的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2903166/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com