Uses import some.directory. * worse for performance?

Which is better for performance with

import some.directory.*; 

or

  import some.directory.classNeeded; 

Or does this not affect performance, since the compiler discards libraries that are not used in the class? So it was implemented for convenience?

+3
source share
4 answers

An import statement is completely unnecessary. You can go all your life as a Java developer without writing if you want; it just means that you will be forced to enter the fully qualified class name for each class in your application.

All import allows you to use the short class name in your code instead of the full one (for example, Connection instead of java.sql.Connection ).

If your class has two packages that contain the same short class name, you will have to type both of them all the time to eliminate all ambiguity (e.g. java.sql.Date and java.util.Date ).

Do not confuse import with class loading. This does not affect performance at runtime; this only affects the number of keystrokes that you must enter during development.

+5
source

The import directive is visible only to the compiler to help it distinguish names in different packages. It does not change the generated bytecode. Therefore, there should be no difference in performance.

The reason some people choose not to use

 import some.directory.*; 

this is because it pollutes the namespace with unknown classes and can lead to the accidental use of incorrect classes, although usually the probability of this is very small.

+5
source

Since this is a compiler directive, it does not affect execution performance.

For further reading http://www.javaperformancetuning.com/news/qotm031.shtml

PS, I found this to search for “java import performance” on Google, so maybe next time ...

+2
source

There is no performance in line with this question , just potential naming conflicts.

+1
source

Source: https://habr.com/ru/post/890984/


All Articles