Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance difference between a wild card import and the required class import

What is the complexity in terms of performance between

java.io.* 

and

java.io.File 

PS.

I know that the first one will include every file in java.io.* and the next one only the selected class file.

like image 529
glarkou Avatar asked Aug 19 '11 23:08

glarkou


People also ask

Will the importing of whole package using *) affect the performance instead of importing specific class?

There's no performance impact to using the wildcard since the compiler will only import the classes required by your code.

What is the reasoning to use a specific import instead of a wildcard import?

With specific imports, the class must exist in a package. However, with wildcard imports, particular classes don't need to exist in the package.

Does importing a package have an impact in performance of any program?

There is a misconception like unused imports affect the performance of the code. They won't affect the performance of the application in any way. But in some cases, this may cause conflicts between classes in the namespace.

What are specific and wildcard imports?

Specific imports are hard dependencies, whereas wildcard imports are not. If you specifically import a class, then that class must exist. But if you import a package with a wildcard, no particular classes need to exist. The import statement simply adds the package to the search path when hunting for names.


2 Answers

At runtime 0.

Both generate the same byte code

like image 156
Alexander Pogrebnyak Avatar answered Oct 04 '22 01:10

Alexander Pogrebnyak


Imports are resolved to fully qualified names at compile time. There is no runtime performance difference. If you look at the generated bytecodes, they will be identical.

There might be a small compile time overhead in using one or the other form, but it is likely to be so small that nobody should notice it, let alone care about it.

I know that the first one will include every file in java.io.* and the next one only the selected class file.

Not exactly. What a star import does is to make all of the class names available. The actual classes themselves are not "included" ... in the sense of the C or C++ programming languages.


The real reasons that many people use explicit imports rather than wildcard imports are:

  • Explicit imports clearly document what external classes a class is directly using, provided that you don't leave redundant imports in your code.

  • Explicit imports avoid problems with name collisions arising when you import two packages that contain classes with the same (simple) class name.

  • Explicit imports avoid fragility problems where someone adds a new class to some package that you have wildcard imported. This can lead to new compilation errors in code that previously used to compile, due to a name collision (see previous).

Modern IDEs have accelerators, code elision and other features that help you keep your imports under control if you use explicit imports.

like image 42
Stephen C Avatar answered Oct 04 '22 01:10

Stephen C