一篇文章搞明白Integer、new Integer() 和 int 的概念与区别

简介: 一篇文章搞明白Integer、new Integer() 和 int 的概念与区别

基本概念的区分

1、Integer 是 int 的包装类,int 则是 java 的一种基本数据类型
2、Integer 变量必须实例化后才能使用,而int变量不需要
3、Integer 实际是对象的引用,当new一个 Integer时,实际上是生成一个指针指向此对象;而 int 则是直接存储数据值
4、Integer的默认值是null,int的默认值是0
image

Integer、new Integer() 和 int 的比较

1、两个 new Integer() 变量比较 ,永远是 false
因为new生成的是两个对象,其内存地址不同

Integer i = new Integer(100);
Integer j = new Integer(100);
System.out.print(i == j);  //false

2、Integer变量 和 new Integer() 变量比较 ,永远为 false。
因为 Integer变量 指向的是 java 常量池 中的对象,而 new Integer() 的变量指向 堆中 新建的对象,两者在内存中的地址不同。

Integer i = new Integer(100);
Integer j = 100;
System.out.print(i == j);  //false

3、两个Integer 变量比较,如果两个变量的值在区间-128到127 之间,则比较结果为true,如果两个变量的值不在此区间,则比较结果为 false 。

Integer i = 100;
Integer j = 100;
System.out.print(i == j); //true
Integer i = 128;
Integer j = 128;
System.out.print(i == j); //false

分析:
Integer i = 100 在编译时,会翻译成为 Integer i = Integer.valueOf(100),而 java 对 Integer类型的 valueOf 的定义如下:

public static Integer valueOf(int i){
    assert IntegerCache.high >= 127;
    if (i >= IntegerCache.low && i <= IntegerCache.high){
        return IntegerCache.cache[i + (-IntegerCache.low)];
    }
    return new Integer(i);
}

java对于-128到127之间的数,会进行缓存。
所以 Integer i = 127 时,会将127进行缓存,下次再写Integer j = 127时,就会直接从缓存中取,就不会new了。
4、 int 变量 与 Integer、 new Integer() 比较时,只要两个的值是相等,则为true
因为包装类Integer 和 基本数据类型int 比较时,java会自动拆包装为int ,然后进行比较,实际上就变为两个int变量的比较。

Integer i = new Integer(100); //自动拆箱为 int i=100; 此时,相当于两个int的比较
int j = 100;
System.out.print(i == j); //true

示例1:

public class IntegerDemo {
    public static void main(String[] args) {
        int i = 128;
        Integer i2 = 128;
        Integer i3 = new Integer(128);
        System.out.println("i == i2 = " + (i == i2)); // Integer会自动拆箱为int,所以为true
        System.out.println("i == i3 = " + (i == i3)); // true,理由同上
        Integer i4 = 127;// 编译时被翻译成:Integer i4 = Integer.valueOf(127);
        Integer i5 = 127;
        System.out.println("i4 == i5 = " + (i4 == i5));// true
        Integer i6 = 128;
        Integer i7 = 128;
        System.out.println("i6 == i7 = " + (i6 == i7));// false
        Integer i8 = new Integer(127);
        System.out.println("i5 == i8 = " + (i5 == i8)); // false
        Integer i9 = new Integer(128);
        Integer i10 = new Integer(128);
        System.out.println("i9 == i10 = " + (i9 == i10)); // false
    }
}

答案是

i == i2 = true
i == i3 = true
i4 == i5 = true
i6 == i7 = false
i5 == i8 = false
i9 == i10 = false

示例2:

package demo1.demo1;
public class Campare {
    public static void main(String[] args) {
        Integer a = new Integer(127), b = new Integer(128);
        int c = 127, d = 128, dd = 128;
        Integer e = 127, ee = 127, f = 128, ff = 128;
        System.out.println(a == b); // false 因为a,b都是new出来的对象,地址不同所以为false
        System.out.println(a == c); // true a会自动拆箱为int类型
        System.out.println(a == e); // false 指向的地址不同a是new出来的
        System.out.println(e == c); // true e会自动拆箱为int类型
        System.out.println(e == ee);// true Integer对 处于-128到127范围之间,指向了同一片地址区域
        System.out.println(b == f); // false 指向的地址不同b是new出来的
        System.out.println(f == d); // true f自动拆箱为int类型
        System.out.println(f == ff); /*
                                         * false 指向的不是同一片地址区域。
                                         * 在Integer类型中,-128到127存放的是同一片区域地址,
                                         * 之外的数是另外开辟空间来进行 存储的
                                         */
        System.out.println(d == dd); // true 不解释
    }
}

示例3:

Integer i01 = 59;
int i02 = 59;
Integer i03 =Integer.valueOf(59);
Integer i04 = new Integer(59);

以下输出结果为false的是:

System.out.println(i01== i02);
System.out.println(i01== i03);
System.out.println(i03== i04);
System.out.println(i02== i04);

解析:
i01 == i02 。i01.intValue()i02 两个值的比较5959 -->true;
i01 == i03 。由于 59在-128到127之间,所以,i01和i03的赋值操作返回的是同一个对象。都是从chche中返回的同一个对象,对象地址相同 true;
i03 == i04。i03是来自缓存值,i04是新new的对象 ,二者不是同一个对象,所以false。
i02 == i04。和第一个类似,true。
答案是 C 。
示例4:
与示例3的唯一不同,就是将值全部改成128。

Integer i01 = 128;
int i02 = 128;
Integer i03 = Integer.valueOf(128);
Integer i04 = new Integer(128);

以下输出结果为false的是:

System.out.println(i01 == i02);
System.out.println(i01 == i03);
System.out.println(i03 == i04);
System.out.println(i02 == i04);

答案:

true
false
false
true

最后
欢迎大家一起交流,喜欢文章记得点个赞哟,感谢支持!

相关文章
|
10月前
|
存储 缓存 人工智能
Java int和Integer的区别
本文介绍了Java中int与Integer的区别及==与equals的比较机制。Integer是int的包装类,支持null值。使用==比较时,int直接比较数值,而Integer比较对象地址;在-128至127范围内的Integer值可缓存,超出该范围或使用new创建时则返回不同对象。equals方法则始终比较实际数值。
329 0
|
Linux
linux syscall和int 80的区别
通过以上内容,希望您能更清晰地理解 `int 0x80` 和 `syscall` 的区别及其在不同系统架构中的应用。
1083 99
|
SQL 存储 关系型数据库
int(1) 和 int(10) 有什么区别?
在MySQL中,`int`类型后面的数字(如`int(1)`、`int(10)`)并不影响其存储范围,最大值仍为4294967295(无符号)。这些数字只有在配合`zerofill`使用时才有意义,用于显示时不足位数补0。例如,`int(4) zerofill`会将1显示为0001。这适用于需要固定长度编号的场景,如学号等。
484 3
int(1) 和 int(10) 有什么区别?
|
存储 C语言 Python
[oeasy]python077_int类型怎么用_整数运算_integer_进制转化_int类
本文主要讲解了Python中`int`类型的应用与特性。首先回顾了`int`词根的溯源,探讨了整型变量的概念及命名规则(如匈牙利命名法)。接着分析了整型变量在内存中的存储位置和地址,并通过`type()`和`id()`函数验证其类型和地址。还介绍了整型变量的运算功能,以及如何通过`int()`函数将字符串转化为整数,支持不同进制间的转换(如二进制转十进制)。此外,文章提及了关键字`del`的使用场景,对比了Python与C语言中`int`的区别,并总结了整型与字符串类型的差异,为后续深入学习奠定基础。
348 1
【Java基础面试十一】、int和Integer有什么区别,二者在做==运算时会得到什么结果?
这篇文章解释了Java中`int`基本数据类型和其包装类`Integer`之间的区别,并指出在进行`==`运算时,`Integer`会拆箱为`int`类型,然后比较它们的值是否相等。
【Java基础面试十一】、int和Integer有什么区别,二者在做==运算时会得到什么结果?
|
存储 缓存 Java
int 和 Integer 哪一个占用更多内存?
【8月更文挑战第21天】
707 0
|
缓存 安全 Java
Java的Integer和int有什么区别?
Java的Integer和int有什么区别?
260 1
|
存储 编译器 程序员
int 和 long 的区别
int 和 long 的区别
|
数据采集 分布式计算 数据处理
Dataphin常见问题之与指定类型int不兼容如何解决
Dataphin是阿里云提供的一站式数据处理服务,旨在帮助企业构建一体化的智能数据处理平台。Dataphin整合了数据建模、数据处理、数据开发、数据服务等多个功能,支持企业更高效地进行数据治理和分析。
|
SQL 流计算 OceanBase
OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
【2月更文挑战第25天】OceanBase CDC从热OB库采集过来的Tinyint(1)类型会默认转换成Boolean,请教一下,如果想转换成int类型,有什方法么?
482 3