Java: Integer vs int

摘要:
埃菲尔语言设计人员是这么认为的,而Java设计人员正逐渐接近这个结论。Integer是引用类型,int是本机数据类型。我们在四种情况下讨论它:1)整数和int类型赋值将整数类型赋值为int类型。此时,Integer变量的值将自动解压缩为int类型,然后分配给int变量。这里,底层调用intValue()方法来实现所谓的解包。2) Integer和int类型之间的比较是无关紧要的。Integer==int和int==Integer具有相同的效果。整型变量被解压缩为整型,然后进行比较。如果它们相等,则返回true;否则,返回false。

Definitions

Anintis aprimitive. It isnotanObject. Anintis a high performance, streamlined beast for calculating numbers in the range -2,147,483,648 [-231] aka Integer.MIN_VALUE to +2,147,483,647 [231-1] aka Integer.MAX_VALUE. Anintis a bare bones32-bitchunk of information.intvariables aremutable. Unless you mark themfinal, you can change their value at any time.

AnInteger, is aObjectthat contains a singleintfield. AnIntegeris much bulkier than anint. It is sort like a Fedex box to contain theint.Integers areimmutable. If you want to affect the value of aIntegervariable, the only way is to create anewIntegerobject and discard the old one.

Anintis a number; anIntegeris a pointer that can reference an object that contains a number.

Converting

Fortunately it is easy to convert back and forth betweenintandInteger.

// to int i from Integer ii
int i = ii.intValue();

// to Integer ii from int i
Integer ii = new Integer( i );

See thisAmanuensisfor other conversions.

Why Both?

Why are there bothintandInteger? For speed.ints, without anyObjectpackaging are compact and fast. Would it not have been easier if there were only one of sort of creature that could do everything and have the compiler automatically figure out when the packaging was needed and when not? The Eiffel language designers thought so, and the Java designers are gradually coming around to the same conclusion.

Integer是引用类型,int是原生数据类型。
我们分四种情况来讨论:
1) Integer与int类型的赋值
a.把Integer类型赋值给int类型。此时,int类型变量的值会自动装箱成Integer类型,然后赋给Integer类型的引用,这里底层就是通过调用valueOf()这个方法来实现所谓的装箱的。
b.把int类型赋值给Integer类型。此时,Integer类型变量的值会自动拆箱成int类型,然后赋给int类型的变量,这里底层则是通过调用intValue()方法来实现所谓的拆箱的。
2) Integer与int类型的比较
这里就无所谓是谁与谁比较了,Integer == int与int == Integer的效果是一样的,都会把Integer类型变量拆箱成int类型,然后进行比较,相等则返回true,否则返回false。同样,拆箱调用的还是intValue()方法。
3) Integer之间的比较
这个就相对简单了,直接把两个引用的值(即是存储目标数据的那个地址)进行比较就行了,不用再拆箱、装箱什么的。
4) int之间的比较
这个也一样,直接把两个变量的值进行比较。
值得注意的是:对Integer对象,JVM会自动缓存-128~127范围内的值,所以所有在这个范围内的值相等的Integer对象都会共用一块内存,而不会开辟多个;超出这个范围内的值对应的Integer对象有多少个就开辟多少个内存。

测试代码如下:

public static void main(String[] args) throwsException
    {
        Integer a = 127, b = 128;
        
        int c = 128;
        
        //自动装箱,这里会调用Integer中的valueOf方法把c装箱成Integer类型
        a =c;
        
        //这里比较的是两个对象的地址,显然不等
        System.out.println(a ==b);
        
        //这里会调用Integer的intValue方法获得Integer对象a指向的栈地址中存的值再与c进行值比较,
        //而不是把c装箱成Integer类型进行地址比较
        System.out.println(a ==c);
        
        //同上,也是获得a指向的栈地址中存的值
        System.out.println(c ==a);
        
        /*** 总结:只有在赋值(=)的时候才会出现装箱和拆箱两种情况,
         * 在比较(==)的时候只会出现一种情况,那就是拆箱,而不会出现装箱的情况
         */System.out.println("----------------------------------");
        
        Integer i1 = 127;
        Integer i2 = 127;
        
        System.err.println(i1 ==i2);
        i1 = 128;
        i2 = 128;
        System.err.println(i1 ==i2);
        
        /*** 总结:JVM会自动缓存-128~127范围内的值,所以所有在这个范围内的值相等的Integer对象都会共用一块内存,而不会开辟多个;
         * 超出这个范围内的值对应的Integer对象有多少个就开辟多少个内存,这样做是为了节省内存消耗。
         */}

免责声明:文章转载自《Java: Integer vs int》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇(转)仅供个人学习-接口测试工具:jmeter、postman、soapUI的区别fastjson生成JSON字符串的时候出现$ref下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

随便看看

ES系列二、Mac 通过docker搭建ELK日志收集系统

#检查是否安装了elkdockerimages#清理以前版本的dockerrmi$#安装elk 6.8.0版本的docker pullslasticsearch:6.8.0 dockerpullskibana:68.0dockerpullogstash:68.00#检查dockerimages2是否查看拉取的ElasticSearch:操作命令:docker...

css动画延迟好像有点怪

项目需要使用动画Css。自定义时,会发现设置动画延迟和动画持续时间的总时间不正确,这将导致动画丢失。例如,bounceInLeft动画从左侧出现,然后抖动。当初始动画延迟为0时,动画持续时间为1s,动画已完成,但如果设置该值,动画延迟为1s且动画持续时间是2s,则动画未完成。具体的动画是从左侧出现,然后在1s延迟后直接到达终点,但没有抖动。然后我用w3c写了...

汇编指令MOV

格式:MOVDST,SRC例如:MOVEAX,#050aH;将十六进制050a传送到通用寄存器eax中MOVDI,BXMOVES,AXMOVAX,DSMOVAL,23HMOV[2000H],02HMOV[2061H],BX...

Nginx反向代理缓冲区优化

为了为不同域名的业务需求设置代理_ bufferingproxy_缓冲参数用于控制是否打开后端响应内容的缓冲区_缓冲区将缓冲到硬盘(缓冲区目录由_temp_path命令指定),...

微信小程序通过background-image设置背景图片

微信小程序通过背景图像设置背景:仅支持在线图像和base64图像,不支持本地图像;设置base64图像的步骤如下:1.在网站上http://imgbase64.duoshitong.com/将图片转换为base64格式2的文本。在WXSS中使用上述文本:background image:url(“data:image/png;base64,iVBORw0KG...

Vmware安装Linux打开一直黑屏的四个解决方法

方法1:以管理员身份打开cmd,输入netshwinsocksreset,按enter键,然后重新启动计算机方法2:关闭虚拟机,编辑虚拟机设置,选择硬件中的虚拟机设置、删除加速3D图形前面的复选框,然后再次启动虚拟机。最终的解决方案:我一开始使用的是14版,但上述方法都不能解决黑屏问题。然后我使用版本12来解决这个问题。...