Integer和int使用==比较的总结
public static void main(String[] args) {
int i1 = 128;
Integer i2 = 128;
Integer i3 = new Integer(128);
System.out.println(i1 == i2);//true
System.out.println(i1 == i3);//true
System.out.println("**************************************");
Integer i4 = 127;
Integer i5 = 127;
Integer i6 = 128;
Integer i7 = 128;
System.out.println(i4 == i5);//true
System.out.println(i6 == i7);//false
System.out.println("**************************************");
Integer i8 = new Integer(127);
Integer i9 = new Integer(127);
System.out.println(i8 == i9);//false
System.out.println(i8.equals(i9));//true
System.out.println(i4 == i8);//false
/* Output:
true
true
**************************************
true
false
**************************************
false
true
false
*/
}
- 第5和第6行的结果都为true。因为Integer与int比较时,Ingeger都会自动拆箱(jdk1.5以上)。
- 第12行结果为true,第13行结果为false。
因为Java在编译的时候,Integer i4=127被翻译成-> Integer i4= Integer.valueOf(127);
JDK源码:/**
* Returns an {@code Integer} instance representing the specified
* {@code int} value. If a new {@code Integer} instance is not
* required, this method should generally be used in preference to
* the constructor {@link #Integer(int)}, as this method is likely
* to yield significantly better space and time performance by
* caching frequently requested values.
*
* This method will always cache values in the range -128 to 127,
* inclusive, and may cache other values outside of this range.
*
* @param i an {@code int} value.
* @return an {@code Integer} instance representing {@code i}.
* @since 1.5
*/
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}看一下源码大家就会明白,对于-128到127之间的数,会进行缓存,Integer i6 = 127时,会将127进行缓存,下次再写Integer i7 = 127时,就会直接从缓存中取,就不会new了。
- i8、i9使用的是new, 对象不一样,所以第17行结果为false,第18行结果为true ,第19行结果为false。
总结
- Ingeter是int的包装类,int的初值为0,Ingeter的初值为null。
- 无论如何,Integer与new Integer()不会相等。不会经历拆箱过程,i8的引用指向堆,而i4指向专门存放他的内存(常量池),他们的内存地址不一样,使用 == 比较都为false。
- 两个都是非new出来的Integer,使用 == 比较,如果数在-128到127之间,则是true,否则为false
- 两个都是new出来的,==比较都为false。若要比较值是否相等,需使用equals方法进行比较。
- int和Integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比。
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
Normal
0
7.8 磅
0
2
false
false
false
EN-US
ZH-CN
X-NONE
/* Style Definitions */
table.MsoNormalTable
{mso-style-name:普通表格;
mso-tstyle-rowband-size:0;
mso-tstyle-colband-size:0;
mso-style-noshow:yes;
mso-style-priority:99;
mso-style-parent:"";
mso-padding-alt:0cm 5.4pt 0cm 5.4pt;
mso-para-margin:0cm;
mso-para-margin-bottom:.0001pt;
mso-pagination:widow-orphan;
font-size:10.5pt;
mso-bidi-font-size:11.0pt;
font-family:"Calibri","sans-serif";
mso-ascii-font-family:Calibri;
mso-ascii-theme-font:minor-latin;
mso-hansi-font-family:Calibri;
mso-hansi-theme-font:minor-latin;
mso-bidi-font-family:"Times New Roman";
mso-bidi-theme-font:minor-bidi;
mso-font-kerning:1.0pt;}
最新文章
- WPF这样的界面应该如何编写呢?
- 【DWR系列02】-DWR逆向Ajax即服务器推送
- Mysql 5.7 使用SSL安全连接
- 发布 PM2.5 数据的城市列表
- POJ 3233 Matrix Power Series 矩阵快速幂+二分求和
- javascript 与和非
- MVC 强类型视图
- Oracle基础 (十四)其他函数
- UVA442 Matrix Chain Multiplication 矩阵运算量计算(栈的简单应用)
- [转]单例模式与静态变量在PHP中
- LeetCode 6. ZigZag Conversion Question
- Linux企业级项目实践之网络爬虫(13)——处理user-agent
- C# Socket 简易的图片传输
- IE添加信任站点并设置允许ActiveX控件的VBS脚本
- Git学习 -- 工作区和暂存区
- 先从_proto_下手理解原型--原型学习(一)
- 深入理解redis数据类型
- <;转>;jmeter(十二)关联之正则表达式提取器
- 文件操作 day8
- 解决ubuntu安装系统默认没有创建root用户