I was reading a book about making code cleaner and more readable.
The fact is that, looking at a code from the book, a Java package that ended with an asterisk appeared and they wrote it in this way import java.util.*
. I have always seen packages import java.util
ending in arraylist
, scanner
or date
.
My doubts are what does the asterisk indicate or mean? and what use is it?
The asterisk in Java imports has two functions, although they can be summed up in one: Make all the members of a collection visible without having to use their full name (identifier).
1. Import from a package:
It is the example you have given. If you have to make a small application that makes use of different collections (Set, Map, List) and/or other commonly used classes or interfaces and you are not using an IDE to create it, it is very convenient to write:
instead of the tedious listing:
If you're using an IDE like Eclipse, IntelliJ, or Netbeans, I recommend typing the class name when you use it, hitting Ctrl+ , espacioand letting autocomplete do its magic, automatically including the necessary import for each class. This way you will avoid unnecessary conflicts. For example if you have:
You will find that there are two Date classes imported at once!
2. Import from a class:
There is a second type of import in Java, the static one, in which case what you import are the static methods and attributes of a class:
In this case it is more unlikely to find conflicts, because we do not usually import more than one or two static classes within the same class.
With
You import all classes that depend on
java.util
. Whereas if you reference only one, only that particular one will be imported. For exampleIt is always better to take one
import
of the classes you need. Otherwise we can sometimes find ourselves with unnecessary naming conflicts. Performance is only (minimally) affected on compilation, it has no negative effect on execution.When you use the asterisk at the end of a
import
, you import all the classes inside it, but its use is not recommended because it can cause problems when programming or if you want to reuse the code because it can give you conflicts with otherimport
. For example:Because of this there is a conflict as Java 1.2 added util and contained List as well as awt. It is true that it can be "fixed" since if you put another import of util specifying List it should not give problems but that is already the opinion of each one and in mine having to do import import.* to have to then specify the class loses His point.
In the compilation it will not consume you much as long as it is not a gigantic code and it is full of imports with asterisks.
As you have indicated that you saw it in an example they probably put it to make it more readable. I hope I've helped.
There is something that has not been mentioned in the previous answers and it seems important to me: when we use the * we are only importing the classes of the indicated package, NOT the one of the possible subpackages.
For example, suppose we want to use the Collectors class found in the java.util.stream package.
The following doesn't work since that class is in the stream subpackage.
It is then necessary to navigate through the package hierarchy until you reach the level where the class you need is found.
either
After including any of the two previous imports, we will have visibility over the class.