简体   繁体   English

关于进口声明

[英]about import statement

I want to know where can I check how many classes have been loaded by the class loader. 我想知道在哪里可以检查类加载器已加载了多少个类。 I want to know, whether import statement in the class with * mark will load those many classes in the perm area of heap? 我想知道,带有*标记的类中的import语句是否会在堆的perm区域中加载那么多类? OR only when we declare reference of the specific type then following two classes will be loaded? 或者只有当我们声明特定类型的引用时才会加载以下两个类?
Ex. 防爆。

Map m = new HashMap(); //only these two classes loaded???

what if we write import statement like 如果我们写import语句怎么样?

java.util.*;

will class loader loads all classes under java.util package? 类加载器会加载java.util包下的所有类吗? Is there any class unloading process in java? 在java中有任何类卸载过程吗?

No, import has nothing to do with class loading. 不,导入与类加载无关。 It is just syntax sugar. 它只是语法糖。 The following two programs will produce exactly the same byte code: 以下两个程序将生成完全相同的字节代码:

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.prefs.*;

public class Foo
{
    private Map map = new HashMap ();
}

and

public class Foo
{
    private java.util.Map map = new java.util.HashMap ();
}

The time when class is loaded is up to JVM decision. 加载类的时间取决于JVM决策。 Java specification only guarantees that class will be loaded and initialized before first use. Java规范仅保证在首次使用之前加载和初始化类。

For example one JVM may decide to load all classes directly or indirectly referred by main class recursively before application startup. 例如,一个JVM可能决定在应用程序启动之前递归地直接或间接地引用所有类。 Such behavior could be handy for real-time applications that need to avoid non-deterministic pauses in program execution caused by class loading. 对于需要避免由类加载引起的程序执行中的非确定性暂停的实时应用程序,此类行为可能很方便。 Another JVM may defer class loading until the class is actually used and unload class immediately after it is not used anymore. 另一个JVM可能会延迟类加载,直到实际使用该类,并在不再使用它后立即卸载类。 This may be handy in embedded applications that are concerned by memory footprint. 这在与内存占用有关的嵌入式应用程序中可能很方便。

import ed classes (or wildcards) are not actually loaded by the class loader. import类(或通配符)实际上并未由类加载器加载。 An import statement is merely an "alias" statement (or something like an "implicit prefix" statement in case you import wildcards). import语句只是一个“别名”语句(或者在导入通配符时类似“隐式前缀”语句)。

这个工具VisualVM将帮助您。

If you want to see the classes that are loaded at runtime try this: 如果要查看在运行时加载的类,请尝试以下操作:

java -verbose:class

You should have a look for Question Is there a way to get which classes a ClassLoader has loaded? 您应该查看问题是否有办法获取ClassLoader加载的类?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM