且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

自动装箱与静态数字

更新时间:2023-09-18 09:25:52

基本上它将被编译为:

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.