int和integer的区别
来源:互联网 发布:舒缦水暖床垫淘宝网 编辑:程序博客网 时间:2024/06/05 06:07
Integer和int的区别
1、Integer是int提供的封装类,而int是Java的基本数据类型;
2、Integer默认值是null,而int默认值是0;
3、声明为Integer的变量需要实例化,而声明为int的变量不需要实例化;
4、Integer是对象,用一个引用指向这个对象,而int是基本类型,直接存储数值。
比较:
int i1 = 127;Integer i2 = 127;//java在编译的时候,被翻译成-> Integer i5 = Integer.valueOf(127);Integer i3 = new Integer(127);Integer i4 = 127;int i5 = 128;Integer i6 = 128;
在上面例子中,i1==i2为true , i1==i3为true , i5==i6为false。
因为Integer和int比都会自动拆箱(jdk1.5以上)。其实java在编译Integer i2 = 127的时候,被翻译成-> Integer i2 = Integer.valueOf(127);所以关键就是看valueOf()函数了。只要看看valueOf()函数的源码就会明白了。JDK源码的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);}
看一下源码大家都会明白,对于-128到127之间的数,会进行缓存,Integer i2 = 127时,会将127进行缓存,下次再写Integer i4 = 127时,就会直接从缓存中取,就不会new了。所以i3==i4为true。
摘自:
Integer与int的种种比较你知道多少?
Integer类型与int的==比较
阅读全文
0 0
- int 和 Integer 的区别
- int 和 Integer 的区别
- int和Integer的区别
- int和Integer的区别
- int 和 integer的区别
- Integer和int的区别
- Integer 和 int 的区别
- int和integer的区别
- int和Integer的区别
- int 和integer的区别
- int 和 Integer的区别
- int 和integer的区别
- int 和integer的区别
- Integer和Int的区别
- int 和integer的区别
- int 和integer的区别
- Int 和integer的区别
- int 和Integer的区别
- 大数据 课程
- Linux常用命令
- JavaScript的对象及正则(RegExp)对象
- HDU5963 朋友 博弈
- 0069 drools workbench 在wildfly上的搭建
- int和integer的区别
- [bzoj2038][莫队算法][板子]小Z的袜子
- C#入门作业二补充
- 1038统计同成绩学生(Hash散列)
- 用正则表达式提取信息
- 区间第k大(主席树)
- 数据结构之顺序表的使用
- bzoj2002Bounce 弹飞绵羊 动态树(Link-Cut-Tree)
- SDUT_2122 数据结构实验之链表七:单链表中重复元素的删除