我想知道如果我在我的java程序中包含了很多import
,它会影响我的代码的性能(例如,程序会变慢)吗? Java 中的 import
背后的逻辑与 C 中的 include
相同吗?
最佳答案
would it affect the performance of my code (for example, program will be slower)?
不,它不会影响代码的性能。
二进制文件(类文件)的大小不会增加,因为导入没有通过任何剪切和粘贴机制实现。
它只是一个语法糖,以避免必须编写例如
java.util.List<java.math.BigInteger> myList =
new java.util.ArrayList<java.math.BigInteger>();
这里有一个小测试来证明这一点:
aioobe@e6510:~/tmp$ cat Test.java
import java.util.*;
public class Test {
public static void main(String[] args) {
List<Integer> myInts = new ArrayList<Integer>();
}
}
aioobe@e6510:~/tmp$ javac Test.java
aioobe@e6510:~/tmp$ md5sum Test.class
523036e294b17377b4078ea1cb8e7940 Test.class
(修改Test.java
)
aioobe@e6510:~/tmp$ cat Test.java
public class Test {
public static void main(String[] args) {
java.util.List<Integer> myInts = new java.util.ArrayList<Integer>();
}
}
aioobe@e6510:~/tmp$ javac Test.java
aioobe@e6510:~/tmp$ md5sum Test.class
523036e294b17377b4078ea1cb8e7940 Test.class
Is the logic behind the import in Java the same as include in C?
不,#include
是一个预处理器指令,并通过剪切和粘贴机制实现。
关于Java 导入与代码性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49642901/