• Post Reply Bookmark Topic Watch Topic
  • New Topic

importing entire package or just needed classes  RSS feed

 
JohnV Varghese
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Which is better
import java.util.*;
or
import java.util.List;
with respect to performance!!!
Thanks!!!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kim Kantola
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mani Ram
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 '*'
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!