浅谈Java中Int、Integer、Integer.valueOf()、new Integer()之间的区别
作者:feng的记忆 时间:2023-10-29 20:08:53
Int
Int是Java八种基本数据类型之一,一般大小为4字节32位,取值范围为2-31—231。两个Int类型变量用“==”比较的是值的大小。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
int a = 100;
int b = 100;
System.out.println(a == b);//true
}
}
Integer和Integer.valueOf()
将Int值赋给Integer变量,系统会自动将这个Int值封装成一个Integer对象。
比如:Integer a = 100;实际上的操作是:Integer a = Integer.valueOf(100);
以下是valueOf()的源码
注意:这里Integer.valueOf(),当Int值的范围在-128-127之间时,会通过一个IntegerCache缓存来创建Integer对象;当Int值不在该范围时,直接调用new Integer()来创建对象,因此会出现以下的情况
(1)Integer a = 100; Integer b = 100; a==b结果为true,因为这两个Integer变量引用的是缓存中的同一个Integer对象 ;
(2)Integer c = 200; Integer d = 200; a==b结果为false,因为a和b是通过new Integer() 创建的两个不同对象。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
System.out.println(a == b);//true
System.out.println(c == d);//false
}
}
new Integer()
new Integer()每次都会创建新的对象,==比较的是两个对象的内存地址
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = new Integer(100);
Integer b = new Integer(100);
System.out.println(a == b);//false
}
}
三者之间的比较
(1)不管是new创建的Integer对象,还是通过直接赋值Int值创建的Integer对象,它们与Int类型变量通过“==”进行比较时都会自动拆箱变成Int类型,所以Integer对象和Int变量比较的是内容大小。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
int a = 100;
Integer b = 100;//等价于b=Integer.valueOf(100);
Integer c = new Integer(100);
System.out.println(a == b);//true
System.out.println(a == c);//true
}
}
(2)new创建的Integer对象和直接赋Int值创建的Integer对象使用==比较的是它们的内存地址。
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer b = 100;//等价于b=Integer.valueOf(100);
Integer c = new Integer(100);
System.out.println(b == c);//false
}
}
(3)赋Int值创建的Integer对象比较:
当Int值在-128-127之间时,两个Integer变量引用的是IntegerCache中的同一个对象,内存地址相同,因此==的结果为true;
当Int值不在以上范围时,两个Integer对象都是通过new创建的,内存地址不同,因此==的结果为false
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 100;
Integer b = 100;
Integer c = 200;
Integer d = 200;
Integer f = new Integer(100);
System.out.println(a == b);//true
System.out.println(c == d);//false
System.out.println(a == f);//false
}
}
一个金典面试题
package com.company.algorithm;
public class Main {
public static void main(String[] args) {
Integer a = 49;
int b = 49;
Integer c = Integer.valueOf(49);
Integer d = new Integer(49);
System.out.println(a == b);//true
System.out.println(a == c);//true
System.out.println(b == c);//true
System.out.println(c == d);//false
}
}
来源:https://blog.csdn.net/qq_40742428/article/details/121183574
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 优化之app启动优化的实现
Spring Boot 2.0 配置属性自定义转换的方法
C#无损压缩图片
maven scope provided和runtime的例子说明
![](https://img.aspxhome.com/file/2023/5/88075_0s.jpg)
Java代理模式的深入了解
![](https://img.aspxhome.com/file/2023/0/60570_0s.jpg)
深入解析:打造自动消失的对话框
![](https://img.aspxhome.com/file/2023/6/106436_0s.png)
SpringMVC实现文件上传和下载功能
Java实现的两个线程同时运行案例
![](https://img.aspxhome.com/file/2023/6/64106_0s.png)
Spring Boot日志技术logback原理及配置解析
![](https://img.aspxhome.com/file/2023/2/63422_0s.png)
简单了解java标识符的作用和命名规则
![](https://img.aspxhome.com/file/2023/1/72601_0s.jpg)
浅谈Java由于不当的执行顺序导致的死锁
Java图论进阶之最小生成树算法详解
![](https://img.aspxhome.com/file/2023/4/60464_0s.jpg)
完美解决虚拟按键遮盖底部视图的问题
亲手教你SpringBoot中的多数据源集成问题
C#模板方法模式(Template Method Pattern)实例教程
10个Elasticsearch查询的实用技巧分享
3种Android隐藏顶部状态栏及标题栏的方法
Java并发编程ThreadLocalRandom类详解
基数排序简介及Java语言实现
![](https://img.aspxhome.com/file/2023/8/101758_0s.jpg)
Android GPS详解及示例代码
![](https://img.aspxhome.com/file/2023/9/138779_0s.png)