There is no performance or memory allocation advantage to either -- they both will compile to the same bytecode.
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.
One such example is the java.awt.List
class and the java.util.List
class.
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.*;
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.