2

Is import package.* slower than import package.MyClass? If yes, in which scneario: runtime or compilation?

Gabriel
  • 83
  • 3

4 Answers4

8

import package.* can be slower at compilation time. It can also slow down your IDE, if there are many many things defined in that package.

At runtime, there's no difference.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • `{citationrequired}` - have you got any evidence that it's slower at compile time? – skaffman Jan 26 '10 at 17:25
  • You mean `{{citation needed}}` :P No, I have no such evidence at hand. I have noticed the IDE slowdown first-hand, though. – Thomas Jan 26 '10 at 17:33
  • Oh, my citation just appeared: http://stackoverflow.com/questions/1983435/eclipse-java-is-it-harmful-to-import-java-namespace/1984499#1984499 – Thomas Jan 26 '10 at 17:36
4

This happens during compilation. If there's a speed difference, it's negligible.

Steve B.
  • 55,454
  • 12
  • 93
  • 132
3

I guess a wildcard import could slow down compilation a bit if the package imported were unusually huge, but in practice, it's negligible.

Import statements have no effect at runtime. The compiled class is identical, whether wildcard or explicit imports are used.

However, wildcard imports are generally discouraged, because they make the code hard to read; it's not clear to a human reader what package a type comes from when multiple packages have been imported this way.

erickson
  • 265,237
  • 58
  • 395
  • 493
  • 1
    Additional reasons why wildcard imports are discouraged: http://javadude.com/articles/importondemandisevil.html – Alistair Sutherland Jan 26 '10 at 17:24
  • RE the link: Quite true, I got burned on that myself. I've also been burned a few times when I imported java.util.* so I could use Date, and then later added java.sql.*, or vice versa. This isn't exactly the same problem as the link describes -- the error turned up when I added the import, not when some other poor sucker checked out my code -- but the underlying issue is the same: You get burned by an ambiguous reference when you don't care about one of the options. That said, I must confess I usually just don't worry about it, and use import something.* whenever I want more than about 3. – Jay Jan 26 '10 at 17:59
  • Modern IDEs (and even some ancient ones) will tell you what package a class is in if you hover over the class name in a declaration. I fail to see how this makes your code less readable. If you have two classes with the same name in different packages, to reduce confusion, you should use the full package name in the object declaration statement, but that's really only necessary if you're using two same-named classes in one class. – belgariontheking Aug 04 '11 at 14:11
2

Because the compiler does some optimizations, it will not be slower at runtime. It could be slower at compile time, but in 99,99% of the cases, you probably don't care...

Fortega
  • 19,463
  • 14
  • 75
  • 113
  • 1
    It's not really an optimization, it's just that the compiler compiles the fully qualified name into the .class file (import is a compiler directive). – Pascal Thivent Jan 26 '10 at 17:43