当前位置:网站首页>String缓存池与Integer缓存池
String缓存池与Integer缓存池
2022-07-19 05:16:00 【Erdan~】
一、String缓存池
String缓存池是指,在创建字符串对象时,会首先在缓存池中找是否有相同的内容的字符串,如果没有,则会在缓存池中创建新的字符串;使用new关键词一定会创建一个新的对象。
代码如下(示例):
public class Demo01 {
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abc";
String s3 = new String("abc");
System.out.println(s1 == s2); // 结果为true , s1 和 s2 指向同一个地址
System.out.println(s1 == s3); // 结果为false , 使用new关键词创建一个新的对象
}
}
二、Integer缓存池
在创建Integer对象时,Integer内部已经把-127 ~ 128在缓存池中创建好,
如果创建的对象在范围之间,可以使用“==”进行比较,如果超出则要用equals进行比较
代码如下(示例):
public class Test02 {
public static void main(String[] args) {
// 采用Integer类的缓存池(-128 - 127)
Integer i1 = 129;
Integer i2 = 129;
System.out.println(i1 == i2);
Integer n1 = 126;
Integer n2 = 126;
System.out.println(n1 == n2); // 结果为true
Integer i3 = new Integer(12);
Integer i4 = new Integer("12");
System.out.println(i3 == i4); // 结果为false
// 采用Integer类的缓存池(-128 - 127)
Integer i5 = Integer.valueOf(127); // 使用缓存池
Integer i6 = Integer.valueOf("127");
System.out.println(i5 == i6); // 结果true
}
}
边栏推荐
- Learning notes: cat's eye Top100 movie information crawling
- access-list vs ip access-list
- firewall-cmd
- IP---ia复习
- leetcode-序列和为K的数量-对前缀和和哈希代码的分析
- Experience of installing ROS in Jetson nano (failed)
- Leetcode:2. 两数相加【大数加法+模拟进位】
- suse 11 进入单用户
- Leetcode:14. Longest common prefix [thinking + sorting]
- 网安学习(十九)HSRP协议
猜你喜欢
随机推荐
ZTE ZXR10 5250 command hints
Matlab之数据筛选
Abstract抽象类和Interface接口的区别
计算机408+数据库【适合考研复试或期末复习】
Cryptology
Experience of installing ROS in Jetson nano (failed)
Openstack uses dashboard to perform image operations, manage instances, and other operations
Thinkphp5 verification code
Leetcode:20. 有效的括号【三种思路+不同语言实现】
Unity实用框架(四)全局数据管理框架
RDO deployment openstack single node
【PTA】 7-19 支票面额 (15 分)
MongoDB
Leetcode:13.罗马数字转整数【键值对映射】
学习笔记:猫眼top100电影信息爬取
2022 final award modeling notes
Network Security Learning (XXII) build virtual special lines for companies and branches
2022-7-12 第八小组 顾宇佳 (Js)
巴特沃斯(Butterworth)滤波器的设计和幅频特性曲线绘制
网络安全与等级保护