Forums Register Login

importing entire package or just needed classes

+Pie Number of slices to send: Send
Hi,
Which is better
import java.util.*;
or
import java.util.List;
with respect to performance!!!
Thanks!!!
+Pie Number of slices to send: Send
It makes precisely zero difference at runtime. "import" is just a directive to tell the compiler how to interpret your code. By the time the .class files are produced, they contain no record at all of what you imported or how.
Now, does what you import have any impact on the performance of a Java compiler? People have debated this before; generally the answer is that no, it has no noticeable affect. It's not at all like including too many headers in a C program.
Finally, I should point out that style is a more important consideration than performance in this case. Importing single classes (at least, if you're only using a few classes from a package) makes it easier to the reader of your code to know where the various classes you're using came from. Anything that makes it easier to read your code is a Good Thing, single-class imports are generally to be preferred.
+Pie Number of slices to send: Send
I like the method of showing the actual name of the class being imported because in the case of looking at old code, or someone elses code, it helps to know from what packages classes ar imported.
For example, if I wasn't familiar with the SimpleDateFormat class, I could look at the import section of the code, see the import statement and then know where to look for documentation on that class.
Just my opinion.
+Pie Number of slices to send: Send
As Ernest pointed out, it doesn't make any difference.
Personally I will import only the classes. But if I need to import more than 3 classes from the same package, I will do an import '*'
Alas, poor Yorick, he knew this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 866 times.
Similar Threads
importing entire package or just needed classes
Is import java.util.List faster than java.util.*
importing packet
Marcus Exam No2 #57
Doubts in this sample demo class in Java 1.6 Tutorial
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:03:49.