为什么收入后的包装班级工作

发布于 2025-02-10 07:20:51 字数 509 浏览 1 评论 0原文

我正在对某些代码进行审查,并遇到了某人在向成员变量进行插入后的一个实例,该变量是整数周围的包装类别。我自己尝试过,真的很惊讶它起作用。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮萍、无处依 2025-02-17 07:20:51

在平台上使用它是完全安全的。该行为在§15.4>§15.4 .2 Java语言规范(添加了强调):

后缀表达式的结果必须是可转换类型的变量(§5.1.8)到数字类型,或者发生编译时错误。

后缀增量表达式的类型是变量的类型。后修正增量表达式的结果不是变量,而是值。

在运行时,如果对操作数表达式的评估突然完成,则后缀增量表达式突然以相同的原因完成并且不会发生增量。否则,值1将添加到变量的值中,然后将总和存储回变量。在添加之前,二进制数字促销(§5.6.2)在值1和变量的值上执行。如有必要,总和通过缩小原始转换范围(§5.1.3)和/或进行拳击转换(§5.1.7)在存储该变量之前的类型。后缀增量表达式的值是在存储新值之前变量的值。

edit 以下是示例代码中发生的事情的更准确的等效物:

Integer x = 0;
int temp = x.intValue();
x = temp + 1; // autoboxing!
System.out.println(temp + ", ");
System.out.println(x.intValue());

It's perfectly safe to use across platforms. The behavior is specified in §15.4.2 of the Java Language Specification (emphasis added):

The result of the postfix expression must be a variable of a type that is convertible (§5.1.8) to a numeric type, or a compile-time error occurs.

The type of the postfix increment expression is the type of the variable. The result of the postfix increment expression is not a variable, but a value.

At run-time, if evaluation of the operand expression completes abruptly, then the postfix increment expression completes abruptly for the same reason and no incrementation occurs. Otherwise, the value 1 is added to the value of the variable and the sum is stored back into the variable. Before the addition, binary numeric promotion (§5.6.2) is performed on the value 1 and the value of the variable. If necessary, the sum is narrowed by a narrowing primitive conversion (§5.1.3) and/or subjected to boxing conversion (§5.1.7) to the type of the variable before it is stored. The value of the postfix increment expression is the value of the variable before the new value is stored.

EDIT Here's a more accurate equivalent of what's going on in your example code:

Integer x = 0;
int temp = x.intValue();
x = temp + 1; // autoboxing!
System.out.println(temp + ", ");
System.out.println(x.intValue());
输什么也不输骨气 2025-02-17 07:20:51

对于只有几年或几年的编程的程序员来说,TEDD HOPP爵士的答案处于非常复杂的水平。

让我以简单的方式清除您的疑问
假设

    Integer x=10;
    x++;
    System.out.println(x) ;

输出将为11,

因为++的post或pre增量是在内部进行1个添加的添加

IE x+1是必须执行的,并将结果放回同一变量中。

ie x=x+1;

现在我们都知道 +运算符只能采用原语,但是x是对象,
然后,我们有自动拆箱和自动箱概念。
因此,表达式变为

x.intValue()+1;//step 1 auto-unboxing

x=Integer.valueOf(x.intValue()+1);//step 2 auto-boxing

输出,即在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

    Integer x=10;
    x++;
    System.out.println(x) ;

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.

ie x=x+1;

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

x.intValue()+1;//step 1 auto-unboxing

x=Integer.valueOf(x.intValue()+1);//step 2 auto-boxing

Hence output comes as 11 after another step of auto-unboxing inside the println statement.

你与昨日 2025-02-17 07:20:51

从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 type int when necessary. Then the increment operator can work on the resulting int.

踏雪无痕 2025-02-17 07:20:51

这是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

赠佳期 2025-02-17 07:20:51

请注意,如果增量操作移至方法,则输出将如预期:

public void printIncrement(Integer x1) {
    System.out.print(x1++ + ", ");
}

这是因为Java传递了参考的副本,因此您现在正在使用新的参考对象X1而不是X。 X指向的原始整数对象保持不变,X仍指向它。

如果您忘记了方法,则可以在方法中进行计算时很容易咬您。

Note that the output would be as expected if the increment operation were moved to a method:

public void printIncrement(Integer x1) {
    System.out.print(x1++ + ", ");
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文