Java的Integer和int有什么区别
Java是面向对象的编程语言,一切都是对象,但是为了编程的方便还是引入了基本数据类型,为了能够将这些基本数据类型当成对象操作,Java为每一个基本数据类型都引入了对应的包装类型(wrapper class),int的包装类就是Integer,从Java 5开始引入了自动装箱/拆箱机制,使得二者可以相互转换,对应如下:
原始类型:boolean,char,byte,short,int,long,float,double
包装类型:Boolean,Character,Byte,Short,Integer,Long,Float,Double
顺便一提,Java中的基本数据类型只有以上8个,除了基本类型(primitive type),剩下的都是引用类型(reference type)。
所以最基本的一点区别是:Ingeter是int的包装类,int的初值为0,Ingeter的初值为null。除此之外还有区别,请看代码:
public class TestInteger {
public static void main(String[] args) {
int i = 128;
Integer i2 = 128;
Integer i3 = new Integer(128);
System.out.println(i == i2); //Integer会自动拆箱为int,所以为true
System.out.println(i == i3); //true,理由同上
Integer i4 = 127;//编译时被翻译成:Integer i4 = Integer.valueOf(127);
Integer i5 = 127;
System.out.println(i4 == i5);//true
Integer i6 = 128;
Integer i7 = 128;
System.out.println(i6 == i7);//false
Integer i8 = new Integer(127);
System.out.println(i5 == i8); //false
Integer i9 = new Integer(128);
Integer i10 = new Integer(123);
System.out.println(i9 == i10); //false
}
}
为什么i4和i5比是true,而i6和i7比是false呢?关键就是看valueOf()函数了,这个函数对于-128到127之间的数,会进行缓存, Integer i5 = 127时,会将127进行缓存,下次再写Integer i6 = 127时,就会直接从缓存中取,就不会new了。所以i4和i5比是true,而i6和i7比是false。
而对于后边的i5和i8,以及i9和i10,因为对象不一样,所以为false。
以上的情况总结如下:
1,无论如何,Integer与new Integer不会相等。不会经历拆箱过程,new出来的对象存放在堆,而非new的Integer常量则在常量池(在方法区),他们的内存地址不一样,所以为false。
2,两个都是非new出来的Integer,如果数在-128到127之间,则是true,否则为false。因为java在编译Integer i2 = 128的时候,被翻译成:Integer i2 = Integer.valueOf(128);而valueOf()函数会对-128到127之间的数进行缓存。
3,两个都是new出来的,都为false。还是内存地址不一样。
4,int和Integer(无论new否)比,都为true,因为会把Integer自动拆箱为int再去比。
Java的Integer和int有什么区别的更多相关文章
- Integer与int有什么区别?
Integer与int有什么区别? 由于面试的时候问到这个问题,所以就网上百度一下,发现一个大神说得非常好,非常清楚,所有就博文复制过来供“自己学习”.(这不是原文,原文底下有链接) ...
- java中Integer与int装箱拆箱一点收获
示例代码: class BoxIntInteger { public static void main(String[] args) { Integer a = new Integer(10111); ...
- java中Integer和int的区别(转)
int和Integer的区别 1.Integer是int的包装类,int则是java的一种基本数据类型 2.Integer变量必须实例化后才能使用,而int变量不需要 3.Integer实际是对象的引 ...
- Java|从Integer和int的区别认识包装类
https://blog.csdn.net/darlingwood2013/article/details/96969339?utm_medium=distribute.pc_relevant.non ...
- Java中Integer 和 int的区别
基本概念的区分: 1.Integer 是 int 的包装类,int 则是 java 的一种基本数据类型 2.Integer 变量必须实例化后才能使用,而int变量不需要 3.Integer 实际是对象 ...
- Java中Integer和int的异同
public void Test1() { int a = 128; Integer b = 128; Integer c = 128; //Integer会自动拆箱成int,所以为ture Syst ...
- Java的Integer与int互转
int转Integer ; Integer wrapperi = new Integer(i); Integer转int ); int i = wrapperi.intValue(); JDK1.5以 ...
- Java中Integer与int对比的一些坑
Integer与int类型的关系 Integer是int的包装类,int的默认值是0,而Integer的默认值是null(我们经常在代码中使用的Integer.valueOf() 和xx.intVal ...
- java中Integer和int的区别
亲看这里 例子: public class Test { public static void main(String[] args) { Integer i = new Integer(128); ...
随机推荐
- 关于Unity中新版动画系统的使用
Mecanim动画 1:旧版动画系统只能通过代码来控制动画播放,随着动画种类变多,代码复杂度也会增加,同时动画过渡也需要非常繁琐的代码控制,为了让有经验的动画师开发动画,unity推出了针对人物角色的 ...
- 如何安装Node.js环境
一.在Windows系统下安装Node.js环境 1. 下载地址:官网http://nodejs.org/ 2. 安装nodejs,根据自己情况,安装路径 D:\Program Files\nod ...
- static为什么一般与final一起用?
static和final的意义是不同的,static修饰的时候代表对象是静态的,而final修饰的时候代表对象只能赋值一次,他们连用的时候是因为定义的那个对象既要它是静态的,也要求它的值不能再被修改. ...
- windows sever2003安装Wamp 2.5不成功——VC 11不支持Windows Server 2003和win XP?
Windows Server 2003 安装Wamp 2.5不成功Wampserver 2.5 will not run on Windows XP 报错信息:C:\wamp\bin\apache\a ...
- Eclipse中创建Maven项目失败
Eclipse中创建Maven项目报错:Unable to create project from archetype org.apache.maven.archetypes:maven-archet ...
- 转载:手把手教你做iOS推送
手把手教你做iOS推送 http://www.cocoachina.com/industry/20130321/5862.html
- 超炫酷的jQuery/HTML5应用效果及源码
jQuery非常强大,我们之前也用jQuery分享过很多实用的插件.HTML5可以让网页变得更加绚丽多彩,将HTML5和jQuery结合使用那将发挥更棒的效果. 今天向大家收集了一些关于HTML5和j ...
- 一篇关于apache commons类库的详解[转]
1.1. 开篇 在Java的世界,有很多(成千上万)开源的框架,有成功的,也有不那么成功的,有声名显赫的,也有默默无闻的.在我看来,成功而默默无闻的那些框架值得我们格外的尊敬和关注,Jakarta C ...
- “A configuration with this name already exists” error in eclipse run configurations
“A configuration with this name already exists” error in eclipse run configurations Is there a way t ...
- USBWebServer 中文便携版 快速搭建 PHP/MySQL 网站服务器环境
如果你是一位 WEB 开发者,或正在学习网页编程,你一定会发现,每到一台新电脑上想要在本地调试测试/运行网站代码都得搭建配置一遍 WAMP (Win.Apache.PHP.MySQL) 环境简直烦透了 ...