简体   繁体   中英

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 on performance as the compiler discounts libraries that aren't used with in the class? So it was implemented for convenience?

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.

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 (eg, 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 (eg, 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.

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...

根据这个问题没有性能命中,只是潜在的命名冲突。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM