Java语言中的import语句的含义

前端之家收集整理的这篇文章主要介绍了Java语言中的import语句的含义前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当我们在 Java文件中使用import语句时,任何人可以清楚地向我解释究竟发生了什么?如果我们添加越来越多的java类,它会增加文件的大小吗?为什么我们不使用类加载器呢?进口有什么限制?

解决方法

导入声明(not语句)本质上是源代码级别的短期使能器:它允许您使用单个标识符(例如List,min)引用类型或静态成员,而不是完全限定名称(例如java .util.List,Math.min).

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

类加载器用于完全不同的概念,与导入功能无关.

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.

参考

> 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

也可以看看

> Java Tutorials/Using package members
> Java Language Guide/static import

各种进口相关问题

关于进口的语法作用:

> What is an import called? – 这是一个声明,而不是声明

按需点对单一类型:

> 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.*; and import java.util.Date;?

导入静态:

> What does the static modifier after import 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? – 绝对不!
> Do multiple import statements in a program affect performance? – NOPE!
> Any reason to clean up unused imports in Java,other than reducing clutter?

猜你在找的Java相关文章