In Java we can either import single classes as well as the whole set of classes (a package).
As an example
import java.util.*
includes
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Iterator;
import java.util.Map;
Other than the length of the code, are there any specific advantages of using the each approach in any manner? Memory allocation? Performance?
解决方案
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.