Can additional import in Java slow down code loading time?

Is it possible that adding additional import statements to java code can slow down the time it takes to load your classes into the JVM?

+7
java
source share
4 answers

No, import is used only in compilation to search for references to classes. Add unused imports and they do nothing. In other words:

import java.util.*; 

just means you can write:

 Map map = new HashMap(); 

instead:

 java.util.Map map = new java.util.HashMap(); 

What does all this do.

+15
source share

Not. Import is just a compilation of time ... syntactic sugar.

Import tells the Java compiler how to map identifiers in the source code to fully qualified class names. But if the source code does not use the imported class, the bytecode file will not have references to it. Therefore, excess import does not (and cannot) affect class loading time.

+5
source share

Import can affect compile time, but not load time or runtime. Basically, if you import classes that you donโ€™t need (usually by importing wildcards when explicit imports will be performed), you can slow down the compiler a bit.

However, even this effect is usually trivial if you do not compile a huge system.

+5
source share

Do not confuse the word import with class loading. The import statement does not load any code into memory. This is just a convenience, allowing you to refer to classes using their short name instead of entering the full name of the class (for example, "Connection" instead of "java.sql.Connection").

+4
source share

All Articles