Java 中冗余导入语句的影响是什么? [英] what is the impact of redundant import statements in Java?

查看:53
本文介绍了Java 中冗余导入语句的影响是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

冗余 java import 语句会影响什么?

What is effected by redundant java import statements?

它们是否影响编译的运行时(性能/大小)?或者只是像智能感知之类的东西?

Do they effect the compiled runtime (performance/size)? or just stuff like intellisense?

换个说法:移除它们有多重要?

To ask differently: how important is it to remove them?

推荐答案

Import 语句只影响编译时发生的事情.

Import statements only affect what happens during compile time.

编译器接受此代码,并创建一个 .class 文件,该文件以可执行格式(二进制)表示您的代码.

The compiler takes this code, and creates a .class file that represents your code in an executable format (something in binary).

最终,二进制文件完全相同,但它们的制作方法不同.

In the end, the binaries are exactly the same, but the method by which they are made are different.

我们来看一个简单的案例:

Let's look at a simple case:

import java.util.*;

对比

import java.util.ArrayList;
import java.util.List;

用于:

//...
List <String> someList = new ArrayList <String> ();
//...

当编译器遇到List 这个词时,在第一种情况下,它需要确定List 是否存在于该组类中.在第二种情况下,它已经明确给出了,所以它更容易.

When the compiler hits the word List, in the first case, it will need to figure out if List exists in that set of classes or not. In the second case, it is already given it explicitly, so its much easier.

本质上,编译器必须获取存在于 import 语句中的所有类并跟踪它们的名称,这样,如果您使用它,编译器就可以检索适当的函数你正在打电话.

In essence, what happens is the compiler must take all the classes existing in the import statements and keep track of their names so that, if you use it, the compiler can then retrieve the appropriate functions that you are calling.

有时,在多个包中有同名的类.在这种情况下(Thomas 指的是),您不应该使用 * 来选择目录中的所有类.

Sometimes, there are classes that have the same name in multiple packages. It is in this case (which Thomas is referring to) that you should not use the * to select all the classes in the directory.

最好的做法是明确描述您的类使用情况.

It is best practice to explicitly describe your class usage.

这篇关于Java 中冗余导入语句的影响是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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