问题描述
我知道我们编写的每个java程序都会自动导入包 java.lang
,因此我们可以自动使用它中的所有类。
I know that the package java.lang
is auto-imported by every java program we write, hence all the classes in it are automatically available to us.
我的问题是为什么不自动导入 java.util
和其他包呢?这肯定会节省一些打字:)
My question is why not auto import java.util
and other packages too? That sure will save some typing :)
所以请解释为什么没有这样做。
So please explain why is this not done.
推荐答案
不自动导入太多的一个好理由是避免命名空间冲突。如果 java.util
中的所有内容都是自动导入的,然后你想引用另一个名为Map的类,那么你必须完全引用它 - 限定名称。
A good reason not to autoimport too much is to avoid namespace clashes. If everything in java.util
was imported automatically and then you wanted to refer to a different class named 'Map', for example, you would have to refer to it by its fully-qualified name.
为回应此线程中的其他答案, import
实际上并未修改内部表示形式你的班级文件。事实上,这是一个到描述类文件结构的JVM规范:看看导入没有存储在任何地方。
In response to other answers in this thread, import
does not actually modify the internal representation of your class files. In fact, here is a link to the JVM spec describing the class file structure: see that imports are not stored anywhere.
这篇关于为什么autoimport只有java.lang包?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!