Java 导入 package.* 与导入 package.SpecificType
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/187453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Import package.* vs import package.SpecificType
提问by Juan Carlos Blanco Martínez
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?
是否会假设在编写一个加载一个包中的所有类型的导入的开销方面有什么不同(import java.*
)?不仅仅是特定类型(即import java.lang.ClassLoader
)?第二种是比另一种更可取的使用方式吗?
采纳答案by shsteimer
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.
执行 import .* 与导入特定类型没有性能或开销成本。但是,我认为永远不要使用 import 是最佳实践。* 我这样做的主要原因是我只是喜欢让事情保持直截了当,干净并且尽可能少含糊,我认为使用 .* import 你会失去这一点.
回答by VonC
A good reason to never use import xxx.* is to have a clear vision of dependencies.
永远不要使用 import xxx.* 的一个很好的理由是对依赖关系有一个清晰的认识。
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.
您可以更快地知道您正在使用另一个包的特定类,因为它列在源文件的开头。
回答by Chris Cudmore
Take a look at the java API, and you'll see many classes and interfaces with the same name in different packages.
看看java API,你会在不同的包中看到许多同名的类和接口。
For example:
例如:
java.lang.reflect.Array
java.sql.Array
So, if you import java.lang.reflect.*
and java.sql.*
you'll have a collision on the Array type, and have to fully qualify them in your code.
因此,如果您导入java.lang.reflect.*
并且java.sql.*
您将在 Array 类型上发生冲突,并且必须在您的代码中完全限定它们。
Importing specific classes instead will save you this hassle.
相反,导入特定的类将为您省去这个麻烦。
回答by Juan Carlos Blanco Martínez
After looking for further information, I came across this website where it is very well explained. Import issueand 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.
这两种风格之间是否存在效率问题?可能,但由于导入声明实际上并未将任何内容导入您的程序,因此任何差异都非常小。请记住,在编译单元的顶部有一个隐式导入 java.lang.*,JDK 1.2.2 中的 java.lang 包含 75 个类和接口。一个使用人为示例的实验,其中有数千个必须查找的类名用途,显示编译速度的变化可以忽略不计。因此,在选择一种格式而不是另一种格式时,编译性能可能不应被视为一个因素。
There's one final angle of interest on import declarations. Suppose you use an inner class:
关于进口申报还有最后一个有趣的角度。假设您使用内部类:
package P;
public class A {
public static class B {}
}
If you want to access A from another compilation unit, you say:
如果你想从另一个编译单元访问 A,你说:
import P.*;
or: import P.A; But if you'd like to access B without qualification, you need to say:
或:进口PA;但是如果你想无条件访问B,你需要说:
import P.A.*;
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.
或:进口PAB;第一个使包 P 中的 A 类中的类型可用。 第二个使包 P 中的 A 类中的类型 B 可用。
回答by Rontologist
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.
我倾向于使用 IDE 的默认设置。我发现这并不值得担心,因为它没有性能影响,并且可以使用各种工具处理依赖项检查。
回答by Alex Miller
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.
我发现最好是:a) 通过列出所有导入来明确 b) 让您的 IDE 管理它。任何主要的 IDE 都可以自动更新、排序和完成您的导入。
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.
我发现 a) 在 IDE 内重构的上下文之外进行手动重新打包时会派上用场几次。例如,当营销更改您的产品名称并决定您的所有包装都应更改名称时。
回答by Scott Stanchfield
This is actually a very bad problem.
这实际上是一个非常糟糕的问题。
Suppose you write
假设你写
import a.*;
import b.*;
...
Foo f;
and class Foo exists in package a.
并且类 Foo 存在于包 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).
现在您检入您完美编译的代码,六个月后,有人将类 Foo 添加到包 b 中。(也许它是在最新版本中添加类的第三方库)。
Poof! Now your code refuses to compile.
噗!现在您的代码拒绝编译。
Neveruse import-on-demand. It's evil!
永远不要使用按需导入。这是邪恶的!
See http://javadude.com/articles/importondemandisevil.htmlfor more details.
有关更多详细信息,请参阅http://javadude.com/articles/importondemandisevil.html。
RE performance:
再生性能:
import a.*;
vs
对比
import a.X;
Makes no difference at runtime. The compiler hardwires the resolved class names into the generated .class files.
在运行时没有区别。编译器将解析的类名硬连接到生成的 .class 文件中。
回答by Dan Rosenstark
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 dowhen I look at a class, to answer the question: Who does it talk to?
少数派观点:在我的代码中,我倾向于使用来自几个包的大量类以及一些奇怪的类。我喜欢保持我的进口清单很小,这样我就可以一目了然地知道发生了什么。为此,我将阈值设置为 4 个类。在此之上,Eclipse 将使用 * 作为我的代码。我发现这使我的包导入保持可读性,并且我倾向于将它们作为我查看类时做的第一件事,以回答以下问题:它与谁交谈?
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).
关于名称冲突:从具有竞争类名称的两个包中导入四个或更多类的可能性有多大?如果超过 10% 的时间,您可能需要考虑您的类所依赖的包的数量(例如,将其重构为更小的类)。
回答by defectivehalt
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.
这更像是一种很好的编码实践,因为任何阅读您的代码的人都会通过查看文件顶部的导入块立即知道特定类使用了哪些类,而人们则必须深入了解您是否使用了通配符。
回答by Onur
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.
如果要从同一个包中导入 20 多个类,最好使用 import xxx.*。“清洁代码”也支持导入整个包。