在Java中,未使用的导入声明是否会占用内存? [英] Does an unused import declaration eat memory, in Java?

查看:591
本文介绍了在Java中,未使用的导入声明是否会占用内存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个未使用的导入是这样的 - import android.widget.RelativeLayout; 吃内存吗?
只是想知道它有多少或者只是有价值?
也许这是个愚蠢的问题,但我还没有找到答案。

Does an unused import like so - import android.widget.RelativeLayout; eat memory? Just want to know about how much or just is it valuable? Maybe this is stupid question, but I haven't found answer.

推荐答案

不,他们不接受任何答案。记忆。编译器只使用导入来在编译时解析类名。

No they don't take any memory. Imports are just used by compiler to resolve class names at compile time.

编译器将每个类名更改为完全限定名。并删除import语句。因此,import语句不会使其成为字节代码。

Compiler changes each class name to fully qualified name. And removes the import statement. So, the import statement doesn't make it to byte code.

唯一可以出现通配符导入的问题是名称空间冲突,即,当两种类型与同名在两个不同的包中定义,然后使用通配符导入这些包将导致使用该类型的名称冲突。

The only issue that can come up with wildcard import is namespace conflict, i.e., when two types with the same name is defined in two different packages, then importing those packages with wildcards will cause name conflict for that type used.

要查看编译器如何替换import语句,可以使用 javap 命令生成类的字节代码。请考虑以下代码:

To see how compiler replaces the import statement, you can generate the byte code of your class using javap command. Consider the below code:

import java.util.*;
import java.util.regex.*;

public class Test {
    public static void main(String[] args) {

    }
}

只需编译上面的代码,并使用以下命令检查字节代码:

Just compile the above code, and check the byte code using the following command:

javap Test

它给出以下输出:

public class Test {
  public Test();
  public static void main(java.lang.String[]);
}

所以,你可以看到 String type被替换为完全限定名称 java.lang.String ,并且字节码中没有import语句。

So, you can see that String type is replaced with it's fully qualified name java.lang.String, and there is no import statement in byte code.

这篇关于在Java中,未使用的导入声明是否会占用内存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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