简体   繁体   中英

Could extra imports in Java slow down code loading time?

在Java代码中添加更多import语句是否有可能减慢将类加载到JVM所需的时间?

No, imports are only used in compilation to find class references. Add unused imports and they don't do anything. To put it another way:

import java.util.*;

simply means you can write:

Map map = new HashMap();

instead of:

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

That's all it does.

No. Imports are purely a compile time construct ... syntactic sugar.

The imports tell the Java compiler how to map identifiers in the source code to fully qualified class names. But if the source code does not use an imported class, the bytecode file will have no references to it. Hence, a redundant import does not (and cannot) impact on class load times.

Imports can have an effect on compilation time, but not on loading time or running time. Basically, if you import classes that you don't need (typically by using wildcard imports when explicit imports would do), then you can slow the compiler a bit.

However, even that effect is generally trivial unless you are compiling a huge system.

Don't confuse the word "import" with "class loading". The import statement does not cause any code to be loaded into memory. It's just a convenience that allows you to refer to classes using their short name instead of typing the full class name (eg, "Connection" instead of "java.sql.Connection").

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