更新时间:2021-10-01 10:08:02
两者都没有性能或内存分配优势 - 它们都会编译为相同的字节码。
There is no performance or memory allocation advantage to either -- they both will compile to the same bytecode.
import
语句是告诉编译器在哪里找到源代码所引用的类。
The import
statement is to tell the compiler where to find the classes that the source code is referring to.
但是,仅按类导入是有利的。如果在两个包中有一个具有完全相同名称的类,则会引起关于引用哪个类的冲突。
However, there is an advantage to importing only by classes. If there is a class with the exact same name in two packages, there is going to be a conflict as to which class is being referred to.
一个这样的例子是 java.awt.List
class和 java.util.List
class。
One such example is the java.awt.List
class and the java.util.List
class.
假设我们要使用 java.awt.Panel
和 java.util.List
。如果源导入包如下:
Let's say that we want to use a java.awt.Panel
and a java.util.List
. If the source imports the packages as follows:
import java.awt.*;
import java.util.*;
然后,参考 List
类是将是一个暧昧的:
Then, referring to the List
class is going to be ambigious:
List list; // Which "List" is this from? java.util? java.awt?
但是,如果明确导入,则结果为:
However, if one imports explicitly, then the result will be:
import java.awt.Panel;
import java.util.List;
List list; // No ambiguity here -- it refers to java.util.List.