导入包.* 与导入包.SpecificType
编写一个导入来加载一个包中的所有类型(import java.*
)的开销是否会存在任何差异? 不仅仅是一个特定的类型(即import java.lang.ClassLoader
)? 第二种使用方式会比另一种更明智吗?
Would it suppose any difference regarding overhead to write an import loading all the types within one package (import java.*
); than just a specific type (i.e. import java.lang.ClassLoader
)? Would the second one be a more advisable way to use than the other one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
少数派观点:在我的代码中,我倾向于使用来自几个包的大量类以及一些奇怪的类。 我喜欢保持我的进口清单很小,这样我就能一眼看出发生了什么。 为此,我将阈值设置为 4 个类别。 除此之外,Eclipse 将使用 * 作为我的代码。 我发现这使我的包导入保持可读性,并且当我查看一个类时,我倾向于将它们称为我做的第一件事,以回答这个问题:它与谁交谈?
关于名称冲突:从两个具有竞争类名的包中导入四个或更多类的可能性有多大? 如果超过 10% 的时间,您可能需要考虑您的类所依赖的包的数量(例如,将其重构为更小的类)。
Minority view: in my code I tend to use tons of classes from a few packages along with a few odd classes here and there. I like to keep my imports list small so I can tell what is going on at a glance. To do this, I set the threshold at 4 classes. Above that, Eclipse will use * for my code. I find this keeps my package imports readable, and I tend to refer to them as the first thing I do when I look at a class, to answer the question: Who does it talk to?
Regarding Name Collision: What are the chances that you import four or more classes from two packages that have competing class names? IF it's more than 10% of the time, you might want to consider the number of packages your class relies on (e.g., refactor it into smaller classes).
永远不要使用 import xxx.* 的一个很好的理由是对依赖关系有一个清晰的认识。
您可以更快地知道您正在使用另一个包的特定类,因为它列在源文件的开头。
A good reason to never use import xxx.* is to have a clear vision of dependencies.
You can know quicker that you are using a specific class of another package because it is listed right at the beginning of the source file.
在寻找更多信息后,我发现了这个网站,它的解释非常清楚。 导入问题 和 在 import 语句中使用 * 会影响性能吗?。
这两种风格之间有效率问题吗? 有可能,但由于导入声明实际上并不将任何内容导入到您的程序中,因此任何差异都非常小。 请记住,编译单元顶部有一个隐式导入 java.lang.*,JDK 1.2.2 中的 java.lang 包含 75 个类和接口。 使用一个人为示例进行的实验(其中必须查找数千个类名使用)显示编译速度的变化可以忽略不计。 因此,在选择一种格式而不是另一种格式时,编译性能可能不应被视为一个因素。
关于进口报关还有最后一个令人感兴趣的方面。 假设您使用内部类:
如果您想从另一个编译单元访问 A,您可以说:
或:
进口PA;
但如果你想无条件访问 B,你需要说:
或:
进口PAB;
其中第一个使包 P 中的类 A 中的类型可用。第二个仅使包 P 中的类 A 中的类型 B 可用。
After looking for further information, I came across this website where it is very well explained. Import issue and Does using * in an import statement affect performance?.
Is there any efficiency issue between these two styles? Possibly, but since import declarations don't actually import anything into your program, any difference is very small. Remember that there's an implicit import java.lang.* at the top of your compilation units, and java.lang in JDK 1.2.2 contains 75 classes and interfaces. An experiment using a contrived example, one with thousands of class name uses that must be looked up, showed a negligible change in compilation speed. So compilation performance should probably not be considered a factor when choosing one format over another.
There's one final angle of interest on import declarations. Suppose you use an inner class:
If you want to access A from another compilation unit, you say:
or:
import P.A;
But if you'd like to access B without qualification, you need to say:
or:
import P.A.B;
The first of these makes available types within the class A found in package P. The second makes available just the type B found in class A in package P.
我倾向于使用 IDE 默认值。 我发现这并不是真正值得担心的事情,因为它对性能没有影响,并且可以使用多种工具来处理依赖关系检查。
I tend to use whatever the IDE default is. I find that it is not something really worth worrying about since it has no performance impact, and checking dependencies can be handled with a variety of tools.
如果要从同一个包导入 20 多个类,最好使用 import xxx.*。 “干净代码”也赞成导入整个包。
If you are importing more than 20 classes from the same package, you are better off using import xxx.*. "Clean Code" is in favor importing the whole package as well.
导入在字节码级别并不重要,因此不应存在运行时差异。
我发现最好:
a) 通过列出所有进口来明确
b) 让您的 IDE 来管理它。 任何主要 IDE 都可以自动更新、排序和完成您的导入。
我发现 a) 在 IDE 内重构的上下文之外进行手动重新打包时可以派上用场几次。 例如,当营销人员更改您的产品名称并决定您的所有包装都应更改名称时。
The imports don't matter at bytecode level, so there should be no runtime difference.
I find it's best to:
a) Be explicit by listing all imports
b) Let your IDE manage it. Any of the major IDEs can automatically update, sort, and complete your imports.
I have found a) to come in handy a couple times when doing manual repackaging outside the context of an in-IDE refactoring. Like for instance, when marketing changes the name of your product and decides all of your packages should change name.
这更像是一种良好的编码实践,因为任何阅读您代码的人只需查看文件顶部的导入块即可立即知道特定类使用了哪些类,而人们必须深入挖掘以找出您是否使用了通配符。
It's more of a good coding practice as anyone reading your code will immediately know what classes are used by a particular class by just looking at the import block at the top of the file whereas one would have to dig to find out if you used wildcards.
看一下java API,你会看到许多类和接口在不同的包中具有相同的名称。
例如:
因此,如果您导入
java.lang.reflect.*
和java.sql.*
,您将在 Array 类型上发生冲突,并且必须完全在您的代码中限定它们。相反,导入特定的类可以省去这个麻烦。
Take a look at the java API, and you'll see many classes and interfaces with the same name in different packages.
For example:
So, if you import
java.lang.reflect.*
andjava.sql.*
you'll have a collision on the Array type, and have to fully qualify them in your code.Importing specific classes instead will save you this hassle.
与导入特定类型相比,执行 import .* 不会产生性能或开销成本。 然而,我认为最好的做法是永远不要使用 import .* 我这样做的主要原因是我只是想让事情简单、干净并且尽可能少地含糊不清,而且我认为使用 .* import 你会失去这一点。
There is not a performance or overhead cost to doing import .* vs importing specific types. However, I consider it to be a best practice to never use import .* My primary reason for this is I just like to keep things straightward, clean and with as little ambiguity as possible, and I think with a .* import you lose that.
这实际上是一个非常糟糕的问题。
假设您编写
并且类 Foo 存在于包 a 中。
现在您签入完美编译的代码,六个月后,有人将 Foo 类添加到包 b 中。 (也许是第三方库在最新版本中添加了类)。
噗! 现在你的代码拒绝编译。
切勿使用按需导入。 这是邪恶的!
有关更多详细信息,请参阅 http://javadude.com/articles/importondemandisevil.html。
RE 性能:
vs
在运行时没有区别。 编译器将解析的类名硬连接到生成的 .class 文件中。
This is actually a very bad problem.
Suppose you write
and class Foo exists in package a.
Now you check in your perfectly compiling code, and six months later, someone adds class Foo to package b. (Perhaps it's a third party lib that added classes in the latest version).
Poof! Now your code refuses to compile.
Never use import-on-demand. It's evil!
See http://javadude.com/articles/importondemandisevil.html for more details.
RE performance:
vs
Makes no difference at runtime. The compiler hardwires the resolved class names into the generated .class files.