Java导入与代码性能 [英] Java import vs code performance

查看:78
本文介绍了Java导入与代码性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我的java程序中是否包含了许多 import ,它是否会影响我的代码的性能(例如,程序会变慢)? Java中的 import 背后的逻辑是否与C中的 include 相同?

I am wondering if i included many import in my java program, would it affect the performance of my code (for example, program will be slower)? Is the logic behind the import in Java the same as include in C?

推荐答案


会影响我的代码的性能(例如,程序会慢一些吗?)

不,它不会影响代码的性能。

No, it wouldn't affect performance of your code.

二进制文件(类文件)的大小不会增加,因为使用任何剪切和粘贴机制导入的不是

The binaries (the class files) do not increase in size as the import is not implemented with any cut-and-paste mechanism.

它仅仅是一个语法糖,用于避免必须编写例如

It is merely a syntactic sugar for avoiding to have to write for instance

java.util.List<java.math.BigInteger> myList =
        new java.util.ArrayList<java.math.BigInteger>();

这是一个小测试,证明了这一点:

Here is a little test demonstrating this:

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




Java中导入的逻辑是否与包含在C?

不, #include 是预处理程序指令,使用剪切和粘贴机制实现。

No, an #include is a preprocessor directive and is implemented with a cut-and-paste mechanism.

这篇关于Java导入与代码性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆