String类之intern() stringinternpool虚拟机contains
程序员文章站
2022-07-12 20:37:46
...
API说明部分:
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
测试1:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
执行结果:true、false。
表明它们只是字面值相同,实际内存地址并不相同。
而且表明虚拟机不会自动替你完成把字符串加入到字符串常量池的功能。
在内存中,还是生成了两个对象。t1和t2
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
测试2:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
执行结果:true、false、true、false
虽说t1执行了所谓的“扣留”操作,但是并未接受字符串常量池返回的引用对象,导致t1和t2实际的内存地址还是不一样的。
结论:intern()方法如果本身未接受池中返回的对象引用,方法不会更改对象的引用。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
测试3:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
虚拟机默认会把初始化声明的String对象注册到常量池中,当其它字符串.equals(abc)相等时,并调用intern()方法接收返回值,则两个对象的内存地址也将一样,因为引用的都是同一对象地址
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
基础知识,不容忽视!!!
When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.
测试1:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
String abc = "test1"; String bcd = "test2"; String t1 = abc + bcd; String t2 = abc + bcd; System.out.println(t1.equals(t2)); System.out.println(t1 == t2);
执行结果:true、false。
表明它们只是字面值相同,实际内存地址并不相同。
而且表明虚拟机不会自动替你完成把字符串加入到字符串常量池的功能。
在内存中,还是生成了两个对象。t1和t2
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
测试2:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
String abc = "test1"; String bcd = "test2"; String t1 = abc + bcd; String t2 = abc + bcd; System.out.println(t1.equals(t2)); System.out.println(t1 == t2); t1.intern(); t2 = t2.intern(); System.out.println(t1.equals(t2)); System.out.println(t1 == t2);
执行结果:true、false、true、false
虽说t1执行了所谓的“扣留”操作,但是并未接受字符串常量池返回的引用对象,导致t1和t2实际的内存地址还是不一样的。
结论:intern()方法如果本身未接受池中返回的对象引用,方法不会更改对象的引用。
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
测试3:
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
String abc = "test1"; String bcd = abc; bcd = bcd.intern(); System.out.println(abc == bcd);
虚拟机默认会把初始化声明的String对象注册到常量池中,当其它字符串.equals(abc)相等时,并调用intern()方法接收返回值,则两个对象的内存地址也将一样,因为引用的都是同一对象地址
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
基础知识,不容忽视!!!
上一篇: (翻译)Tomcat JDBC 连接池
下一篇: spring 配置连接