Import overhead

Does importing an entire package slow down code?

Q: Does importing an entire package cause any runtime or compile time overhead? For example, does:

import java.util.*

compared with:

import java.util.Vector

cause any added overhead?

A: In answering your questions a few definitions are helpful:

  • Imports of the type java.util.Vector; are referred to as single type import
  • Imports of the form java.util.*; are referred to as import on demand

Both import on demand and single type import are passive mechanisms. As passive mechanisms, import loads information about the package and types only when it is needed. Indeed, the compiler will load this information only when a type is actually used, not when it is simply imported. The import simply tells the compiler where to look when it actually needs a type.

So no, importing an entire package produces no more overhead than importing a specific type.

That being said, it is generally regarded as bad practice to import an entire package using import on demand. Sometimes the number of imports may be large. However, you can quickly look at the list and know what classes the importing class uses. Such a list of imports can provide important documentation to someone unfamiliar with the code.

For more information about the import mechanism, check out:

The Java Programming Language, Third Edition by Ken Arnold, James Gosling, and David Holmes.

Source: www.infoworld.com