运行时常量池的一些理解

public class TestCl{
	
	static final Integer ii = 1000;
	private final Integer b = 1000;
	
	public static void main(String[] args) {
		TestCl cl = new TestCl();
		System.out.println(cl.b == TestCl.ii);
	}
} 


输出false

public class TestCl{
	
	static  Integer ii = 1000;
	private  Integer b = 1000;
	
	public static void main(String[] args) {
		TestCl cl = new TestCl();
		System.out.println(cl.b == TestCl.ii);
	}
}

输出false

比较一二说明final关键字修饰的常量只是不许修改, 不代表把其对象内容放入运行时常量池中

public class TestCl{
	
	static  Integer ii = 1;
	private  Integer b = 1;
	
	public static void main(String[] args) {
		TestCl cl = new TestCl();
		System.out.println(cl.b == TestCl.ii);
	}
}


输出true, 不管是类变量还是成员变量, 其变量引用都指向运行时常量池, 只是成员变量需要类实例化后才进行指向, 类变量在类加载时就已经指向

public class TestCl{
	
	public static void main(String[] args) {
		final TestCl cl = new TestCl();
		final TestCl c2 = new TestCl();
		System.out.println(cl == c2);
	}
}

同理输出false

posted @ 2017-10-14 16:41  車輪の唄  阅读(8)  评论(0编辑  收藏  举报  来源