您的位置:首页 > 编程语言 > Java开发

我的java学习之路疑点小结

2015-04-23 12:31 239 查看
疑点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程序在在它运行之前并非被完全加载,也就不存在因为加载过多不必要的类而导致系统内存溢出。(回收机制也会回收哪些长时间不使用的类)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: