JVM学习分享-思考题
2024-08-25 13:52:58
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* 当调用intern方法时,
* 如果池已经包含此字符串(equals确定),则返回池中字符串。
* 否则,将此String对象添加到池中并且返回该String对象的引用。
*/
public class StringConstantPool_Intern { @Test
public void test1() {
String a1 = "AA";//当调用intern方法时,如果池已经包含此字符串(equals确定),则返回池中字符串。否则,将此String对象添加到池中并且返回该String对象的引用。
a1.intern();
}
}
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* /Users/zero/Code/Myself/Project/zero-Project/jvm/target/classes/zero/desk/stringconstantpool
* javap -verbose StringConstantPoolAA
* cd ../stringconstantpool
*/
public class StringConstantPoolAA { @Test
public void test1() {
String a1 = "AA";//在常量池上创建常量AA
String a2 = "AA";//直接返回已经存在的常量AA
System.out.println(a1 == a2); //true
} }
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* 思考题
*/
public class StringConstantPoolAAAA { @Test
public void test1() {
String a1 = new String("AA");//在堆上创建常量AA
String a11 = a1.intern();
String a2 = "AA";//在常量池上创建常量AA
System.out.println(a1 == a2); //false
System.out.println(a1 == a11); //false
System.out.println(a11 == a2); //true
} }
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* 思考题
*/
public class StringConstantPoolAABB { @Test
public void test1() {
String a1 = "AA"+"BB";//在常量池上创建常量AABB
String a2 = "AABB";//在常量池上创建常量AABB
System.out.println(a1 == a2); //true
} }
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* 思考题
*/
public class StringConstantPoolNAA { @Test
public void test1() {
String a1 = "AA";//在常量池上创建常量AA
String a2 = new String("AA");//在堆上创建对象AA
String a3 = a2.intern();//常量池上存在常量AA,直接返回常量池上常量AA,即a1
System.out.println(a1 == a2); //false
System.out.println(a1 == a3); //true
System.out.println(a2 == a3); //false
System.out.println(System.identityHashCode(a1));
System.out.println(System.identityHashCode(a2));
} }
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* 思考题
*/
public class StringConstantPoolNAABB { @Test
public void test1() {
String a3 = "AA"+new String("BB"); //在堆上创建对象AABB
// String a33 = a3.intern();
String a333 = "AABB";//在常量池上创建常量AABB
System.out.println(a333 == a3); //false
//
System.out.println(System.identityHashCode(a3));
System.out.println(System.identityHashCode(a333));
}
}
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* 思考题
*/
public class StringConstantPoolNAABBi { @Test
public void test1() {
String a3 = "AA"+new String("BB"); //在常量池上创建常量AABB
String a33 = a3.intern();
String a333 = "AABB";//在常量池上创建常量AABB
System.out.println(a333 == a3); //true
//
System.out.println(System.identityHashCode(a3));
System.out.println(System.identityHashCode(a33));
System.out.println(System.identityHashCode(a333));
}
}
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* 思考题
*/
public class StringConstantPoolNAANBB { @Test
public void test1() {
String a3 = new String("AA")+new String("BB"); //在堆上创建对象AABB
// String a33 = a3.intern();
String a333 = "AABB";//在常量池上创建常量AABB
System.out.println(a333 == a3); //false
//
System.out.println(System.identityHashCode(a3));
System.out.println(System.identityHashCode(a333));
}
}
package zero.desk.stringconstantpool; import org.junit.Test; /**
* @author Zero
* @since 2019-09-17.
* Description:
* 思考题
*/
public class StringConstantPoolNAANBBi { @Test
public void test1() {
String a3 = new String("AA")+new String("BB"); //在常量池上创建常量AABB
String a33 = a3.intern();
String a333 = "AABB";//在常量池上创建常量AABB
System.out.println(a333 == a3); //true
//
System.out.println(System.identityHashCode(a3));
System.out.println(System.identityHashCode(a33));
System.out.println(System.identityHashCode(a333));
}
}
最新文章
- jQuery学习之路(7)- 用原生JavaScript实现jQuery的某些简单功能
- win32 应用程序 添加资源
- spring提供的解决中文乱码方案
- Windows Live Writer配置
- Hadoop入门简介
- 关于Java中的构造方法和set方法()给属性赋值
- android 链接蓝牙不稳定的解决建议
- HDU 4704 Sum (费马定理+快速幂)
- html5 01 随记
- 使用svn控制系统的优缺点和注意事项
- Java --Annotation学习心得体会及笔记
- 抓包工具Charles基本用法
- Android studio 远程服务调用
- JavaScript实现文字跑马灯
- 各种MM(存储器)含义
- Chrome浏览器JSON格式化插件
- spring boot 2.0 与FASTDFS进行整合
- hibernate的native sql查询
- Docker实战(一)之使用Docker镜像
- UVa 10917 林中漫步
热门文章
- <;算法基础>;<;排序>;三种高级排序——快速排序,堆排序,归并排序
- 华为云&;#183;寻找黑马程序员#海量数据的分页怎么破?【华为云技术分享】
- 谁说微服务是Spring Cloud的独角戏?Service Mesh了解一下?
- 转:SpringBoot系列: 使用 flyway 管理数据库版本
- CoderForces-Round60D(1117) Magic Gems
- 2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path
- [FPGA]Verilog 60s秒表计时器(最大可计时间长达9min)
- Python 3 对象关系映射(ORM)
- Day 07 字符串内置方法和爬虫基础3
- layui扩展组件,下拉树多选