运行时常量池的一些理解

来源:互联网 发布:使用python ddos攻击 编辑:程序博客网 时间:2024/06/07 08:13
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

原创粉丝点击