开发者

Could extra imports in Java slow down code loading time?

开发者 https://www.devze.com 2022-12-22 17:27 出处:网络
Is it possible that adding more import stat开发者_如何学Pythonements to your java code could slow down the time it takes to load your classes into the JVM?No, imports are only used in compilation to f

Is it possible that adding more import stat开发者_如何学Pythonements to your java code could slow down the time it takes to load your classes into the 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 (e.g, "Connection" instead of "java.sql.Connection").

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号