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.
Solution 1:
At runtime 0.
Both generate the same byte code
Solution 2:
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.
Solution 3:
There is no performance impact on runtime, there might be impact on compilation time: http://www.javaperformancetuning.com/news/qotm031.shtml
Solution 4:
No, there is no affect on run time performance. Because import
statement is a compiler directive and is not converted to the byte code. As stated by @ Stephen C there is only a compile time overhead.
Solution 5:
There is no performance difference between a specific import and a wildcard import declaration.
The information for the classes in imported package is not read in at compile time or run time unless the class is used in the program. The import statement simply tells the compiler where to locate the classes. There is no performance difference between a specific import and a wildcard import declaration.
(Liang, Daniel Y. "Introduction to Computers, Programs and Java." Introduction to Java Programming. Comprehensive Version. 9th ed. N.p.: Pearson, n.d. 24. Print.)