为什么收入后的包装班级工作
我正在对某些代码进行审查,并遇到了某人在向成员变量进行插入后的一个实例,该变量是整数周围的包装类别。我自己尝试过,真的很惊讶它起作用。
Integer x = 0;
System.out.print(x++ + ", ");
System.out.print(x);
这是我预期的,这将打印出0,1
,而不是0,0
。我仔细研究了语言规范,找不到任何涵盖此内容的内容。谁能向我解释为什么这有效以及是否在多个平台上安全?我本以为这会分解成
Integer x = 0;
int temp1 = x.intValue();
int temp2 = temp1 + 1;
System.out.println(temp1);
temp1 = temp2;
System.out.println(x.intValue());
,但显然规范中有一些使它在最后一行之前添加x = temp1;
I was doing a review of some code and came across an instance of someone post-incrementing a member variable that was a wrapper class around Integer. I tried it myself and was genuinely surprised that it works.
Integer x = 0;
System.out.print(x++ + ", ");
System.out.print(x);
This prints out 0, 1
, not 0, 0
as I would have expected. I've looked through the language specification and can't find anything covering this. Can anyone explain to me why this works and if it's safe across multiple platforms? I would have thought that this would decompose into
Integer x = 0;
int temp1 = x.intValue();
int temp2 = temp1 + 1;
System.out.println(temp1);
temp1 = temp2;
System.out.println(x.intValue());
But apparently there's something in the specification that make it add x = temp1;
before the last line
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在平台上使用它是完全安全的。该行为在§15.4>§15.4 .2 Java语言规范(添加了强调):
edit 以下是示例代码中发生的事情的更准确的等效物:
It's perfectly safe to use across platforms. The behavior is specified in §15.4.2 of the Java Language Specification (emphasis added):
EDIT Here's a more accurate equivalent of what's going on in your example code:
对于只有几年或几年的编程的程序员来说,TEDD HOPP爵士的答案处于非常复杂的水平。
让我以简单的方式清除您的疑问
假设
输出将为11,
因为++的post或pre增量是在内部进行1个添加的添加
IE x+1是必须执行的,并将结果放回同一变量中。
现在我们都知道 +运算符只能采用原语,但是x是对象,
然后,我们有自动拆箱和自动箱概念。
因此,表达式变为
输出,即在println语句中自动启动的另一步骤之后,就有11个。
Answer by Sir Tedd Hopp is at a very complex level for programmers who are programming only for few or couple of years.
Let me clear your doubt in simple way
suppose
output will be 11
Because ++ either post or pre increment is doing Addition by 1 only internally
ie x+1 is what it has to perform and put back the result in the same variable.
now we all know that + operator can only take primitives but x is object ,
then we have auto-unboxing and auto-boxing concept.
so the expression becomes
Hence output comes as 11 after another step of auto-unboxing inside the println statement.
从Java 1.5开始,Java执行自动启动以将“包装类型”(例如
Integer
)转换为相应的原始类型int
时。然后,增量运算符可以在结果int
上工作。As of Java 1.5, Java performs auto-unboxing to convert "wrapper types" such as
Integer
to the corresponding primitive typeint
when necessary. Then the increment operator can work on the resultingint
.这是Java 5中的一个“新”功能,称为 - 整数在需要时转换为中,反之亦然。同样适用于浮子,双重,布尔值等。
虽然这是一项方便的功能,但如果您不小心,它可能会导致巨大的性能
It is a "new" feature in Java 5 called autoboxing - Integer is transformed to in when needed - and vice-versa. Same applies to Float, Double, Boolean etc.
While it is a convenient feature, it can lead to a HUGE performance hit if you are not careful
请注意,如果增量操作移至方法,则输出将如预期:
这是因为Java传递了参考的副本,因此您现在正在使用新的参考对象X1而不是X。 X指向的原始整数对象保持不变,X仍指向它。
如果您忘记了方法,则可以在方法中进行计算时很容易咬您。
Note that the output would be as expected if the increment operation were moved to a method:
That's because Java passed a copy of the reference so you're now working with a new reference object, x1 instead of x. The original Integer object that x was pointing to stays unchanged and x still points to it.
This can easily bite you when doing calculations in a method if you forget about it.