在java中的额外进口类的效果 [英] effects of extra imported classes in java

查看:164
本文介绍了在java中的额外进口类的效果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

http://source.android.com/source/$c $ C-style.html

我读到使用完全限定进口。但我好奇的负面影响不使用进口的明确。举例来说,如果我用下面的code

I read about use fully qualify imports. But I am curious about the negative affects of not using imports explicitly. For instance, if I use the following code

package blabla;
import foo.*;
import hee.*;
import lee.*;
...
...
public class ImportKing {
...
}

从我的理解,当这个类 - ImportKing在项目的任何地方使用,类加载器将加载它进口的班,因此将消耗不必要的额外进口的系统内存。

from my understanding, when this class--ImportKing is used anywhere in the project, the classloader will load any classes it imported, therefore will consume extra system memory for needless imports.

测试code:

//import java.*;
public class ImportKing {
    public static void main(String[] args) {
        while(true) {
            System.out.println("running");
        }
    }
}

测试结果是:

USER PID%CPU%MEM VSZ RSS TTY STAT START TIME命令

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND

shanwu 3316 39.8 0.6 3182012 52196点/ 0 + SL 9时25 0点03分的Java ImportKing额外进口

shanwu 3316 39.8 0.6 3182012 52196 pts/0 Sl+ 09:25 0:03 java ImportKing with extra imports

shanwu 3440 43.7 0.6 3182012 52752点/ 0 + SL 0:03 09:27 java的ImportKing无需额外进口

shanwu 3440 43.7 0.6 3182012 52752 pts/0 Sl+ 09:27 0:03 java ImportKing without extra imports

我没有看到额外的进口程序性能有任何负面影响。是额外的进口java编译器处理,所以我们没有像浪费系统内存的问题?

I didn't see any negative effects on program performance for extra imports. Is extra imports handled by java compiler, so we don't have problem like wasting system memory?

推荐答案

问:类加载器将加载它进口的班,

Q: the classloader will load any classes it imported,

答:没有,类加载器将在需要的类(例如,调用构造函数)调用。这有没有关系你的源$ C ​​$ C进口。

A: No, the class loader will be invoked when a class is needed (for example, calling a constructor). This has nothing to do with "imports" in your source code.

问:所以会消耗额外的系统内存,不用进口

Q: therefore will consume extra system memory for needless imports.

答:不:类不加载不必要的;内存的的浪费了。

A: No: classes are not loaded needlessly; memory is not "wasted".

建议:

下面是对Java类加载的基本知识两个很好的链接:

Here are two good links on the basics of Java class loading:

HTTP://www2.sys- con.com/itsg/virtualcd/java/archives/0808/chaudhri/

下面是Java规范有什么看法进口

Here is what the Java specification has to say about "import":

的http://文档。 oracle.com/javase/specs/jls/se7/html/jls-7.html#jls-7.5

这是进口报关使得类型或成员可通过其简单
  只有在实际包含编译单位名称
  进口报关。导入型(S)或部件(多个)的范围
  由进口报关专不包括PACKAGENAME
  一个包声明,其他进口报关单在当前的
  编译单元或其它编译单元在同一封装中。

An import declaration makes types or members available by their simple names only within the compilation unit that actually contains the import declaration. The scope of the type(s) or member(s) introduced by an import declaration specifically does not include the PackageName of a package declaration, other import declarations in the current compilation unit, or other compilation units in the same package.

这是进口是一个编译时间结构来简化识别名与(也许很多不同的一种)类的定义(S)。它的的影响运行时的行为;它是完全的独立的从类装载。

An "import" is a compile-time construct to simplify identifying a "name" with (perhaps one of many different) "class definition(s)". It does not affect runtime behavior; it is completely seperate from "class loading".

这篇关于在java中的额外进口类的效果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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