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.
There's no performance impact to using the wildcard since the compiler will only import the classes required by your code.
With specific imports, the class must exist in a package. However, with wildcard imports, particular classes don't need to exist in the package.
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.
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.
At runtime 0.
Both generate the same byte code
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With