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

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

问题描述

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

解决方案

是否会影响我的代码的性能(例如,程序会变慢)?

不会,它不会影响您的代码的性能.

二进制文件(类文件)的大小不会增加,因为导入没有通过任何剪切和粘贴机制实现.

只是为了避免不得不写的语法糖

java.util.List我的列表 =new java.util.ArrayList();

这里有一个小测试来证明这一点:

aioobe@e6510:~/tmp$ cat Test.java导入 java.util.*;公共类测试{公共静态无效主(字符串 [] args){列表<整数>myInts = new ArrayList();}}aioobe@e6510:~/tmp$ javac Test.javaaioobe@e6510:~/tmp$ md5sum Test.class523036e294b17377b4078ea1cb8e7940 Test.class

(修改Test.java)

aioobe@e6510:~/tmp$ cat Test.java公共类测试{公共静态无效主(字符串 [] args){java.util.ListmyInts = new java.util.ArrayList();}}aioobe@e6510:~/tmp$ javac Test.javaaioobe@e6510:~/tmp$ md5sum Test.class523036e294b17377b4078ea1cb8e7940 Test.class

<块引用>

Java 中导入背后的逻辑与 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?

解决方案

would it affect the performance of my code (for example, program will be slower)?

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

(modifying 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?

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

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

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