我的java学习之路疑点小结

来源:互联网 发布:网络投资电信诈骗 编辑:程序博客网 时间:2024/05/24 04:20

疑点1:编写一个java文件的时候,


疑点2:一个程序中有很多的类,运行的时候(例如在tomcat上),是否加载所有的类?如果那样的话当程序太大的时候会不会内存溢出?

解答:

package ClassLoad;public class Test{    public static void main(String[] args)    {        System.out.println("------------------begin--------------------");        new Gum();        try{            Class.forName("ClassLoad.Cookie");        }catch(Exception e)        {            throw new RuntimeException(e);        }        System.out.println("After Class.forName('Cookie')");        new SweetShop();        System.out.println("------------------end--------------------");    }}class Gum{    static{        System.out.println("Gum.enclosing_method()");    }}class Cookie{    static{        System.out.println("Cookie.enclosing_method()");    }}class SweetShop{    static{        System.out.println("SweetShop.enclosing_method()");    }}
经过验证,可知,所有的类都是在第一次使用是动态加载到JVM中的,java程序在在它运行之前并非被完全加载,也就不存在因为加载过多不必要的类而导致系统内存溢出。(回收机制也会回收哪些长时间不使用的类)

0 0
原创粉丝点击