is using import some.directory.* worse for performance?
What is better for performance using
import some.directory.*;
or
import some.directory.classNeeded;
Or does this not make any change 开发者_如何学Con performance as the compiler discounts libraries that aren't used with in the class? So it was implemented for convenience?
The import statement is completely unnecessary. You can go your entire life as a Java developer without writing one if you wish; it just means that you'll be forced to type out the fully-qualified class name for every class in your application.
All import
does is allow you to use the short class name in your code instead of the fully qualified one (e.g., Connection
instead of java.sql.Connection
).
If your class has two packages that include the same short class name, you'll have to type both of them out all the time to eliminate all ambiguity (e.g., java.sql.Date
and java.util.Date
).
Don't confuse import
with class loading. It doesn't impact runtime performance at all; it only influences how many keystrokes you have to type while developing.
The import directive is only visible by the compiler, to help it distinguish between names in different packages. It doesn't change the bytecode generated at all. So there should be no difference in performance.
The reason some people may prefer not to use
import some.directory.*;
is that it pollutes the namespace with unknown classes, and may cause accidental use of the wrong classes, even though usually the chance of this happening is very small.
Since it is a compiler directive, it doesn't affect runtime performance.
For further reading http://www.javaperformancetuning.com/news/qotm031.shtml
PS, i found this looking for "java import performance" on Google, so maybe next time...
精彩评论