自动装箱与静态数字 [英] Auto Boxing vs static numbers

查看:86
本文介绍了自动装箱与静态数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Integer i = NumberUtils.INTEGER_ONE 代替 Integer i = 1 是否有价值?我不知道自动装箱背后会发生什么。

Is it valuable for using Integer i = NumberUtils.INTEGER_ONE instead of Integer i = 1? I don't know what happen behind auto boxing.

谢谢

推荐答案

基本上它将被编译为:

Integer i = Integer.valueOf(NumberUtils.INTEGER_ONE);

假定 INTEGER_ONE 被声明为 int

在执行时,假设INTEGER_ONE的值为1,则实际上将返回对的引用每次相同的对象,由Java语言规范保证,因为它的范围是-128到127。超出该范围的值可以返回对同一对象的引用,但是没有

At execution time, assuming INTEGER_ONE has the value 1, that will actually return a reference to the same object each time, guaranteed by the Java Language Specification, because it's in the range -128 to 127. Values outside that range can return references to the same object, but don't have to.

这篇关于自动装箱与静态数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆