1. Q: Does importing all classes in a package make my object file (.class or .jar) larger?
A: No, import only tells the compiler where to look for symbols.
2. Q: Is it less efficient to import all classes than only the classes I need?
A: No. The search for names is very efficient so there is no effective difference.
3. Q: Doesn’t it provide better documentation to import each class explicitly?
A: This shows good intentions, but …
- It’s hard to remember to remove classes when they are no longer used, so the import list is surprisingly often wrong. It can seriously slow down reading because unusual or unexpected class imports make me look for that class, only to discover that it must have been used in an earlier version.
- Explicit class imports permit accidentally defining classes with names that conflict with the standard library names. This is very bad. Using “*” to import all classes prevents this dangerous naming accident.
- It’s annoying to always update this list, altho if you use NetBeans, fixing the list is only a click away (see below).
4. Q: I’ve imported java.awt.*, why do I also need java.awt.event.*?
A: The wildcard “*” only makes the classes in this package visible, not any of the subpackages.
5. Q: Why don’t I need an import to use String, System, etc?
A: All classes in the java.lang package are visible without an import.
6. Q: Is the order of the imports important?
A: No. Group them for readability.
– by Fred Swart
That sounds nice,indeed
haha, Jack, the 'import' puzzle me for a long time. Finally, I found an answer: there is no performance diff btw explicit import (import java.util.ArrayList;) and wildcard import (import java.util.*;) I asked so many ppl about that (even our lecturer), and got no answer until I met Fred Swart, thx him