在 Java 中,后增量运算符如何在 return 语句中起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/654715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:15:05  来源:igfitidea点击:

In Java, how does a post increment operator act in a return statement?

javaoperators

提问by Hanno Fietz

Given the following code, will ixAdd do what you'd expect, i. e. return the value of ix before the increment, but increment the class member before leaving the function?

给定以下代码,ixAdd 会做您期望的事情,即在增量之前返回 ix 的值,但在离开函数之前增加类成员吗?

class myCounter {
    private int _ix = 1;

    public int ixAdd()
    {
        return _ix++;
    }
}

I wasn't quite sure if the usual rules for post / pre increment would also apply in return statements, when the program leaves the stack frame (or whatever it is in Java) of the function.

当程序离开函数的堆栈帧(或 Java 中的任何内容)时,我不太确定 post/pre 增量的通常规则是否也适用于 return 语句。

回答by Jon Skeet

The key part is that a post increment/decrement happens immediatelyafter the expression is evaluated. Not only does it happen before the return occurs - it happens before any later expressions are evaluated. For instance, suppose you wrote:

关键部分是在计算表达式后立即发生后递增/递减。它不仅发生在返回发生之前 - 它发生在评估任何后续表达式之前。例如,假设您写道:

class myCounter {
    private int _ix = 1;

    public int ixAdd()
    {
        return _ix++ + giveMeZero();
    }

    public int giveMeZero()
    {
        System.out.println(_ix);
        return 0;
    }
}

That would print out the incremented result as well, because the increment happens before giveMeZero()is called.

这也会打印出递增的结果,因为递增发生在giveMeZero()调用之前。

回答by Michael Myers

Well, let's look at the bytecode (use javap -c <classname>to see it yourself):

好,我们来看看字节码(用javap -c <classname>自己看):

Compiled from "PostIncTest.java"
class myCounter extends java.lang.Object{
myCounter();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   aload_0
   5:   iconst_1
   6:   putfield    #2; //Field _ix:I
   9:   return

public int ixAdd();
  Code:
   0:   aload_0
   1:   dup
   2:   getfield    #2; //Field _ix:I
   5:   dup_x1
   6:   iconst_1
   7:   iadd
   8:   putfield    #2; //Field _ix:I
   11:  ireturn

}

As you can see, instructions 6 and 7 in ixAdd()handle the increment before the return. Therefore, as we would expect, the decrement operator does indeed have an effect when used in a return statement. Notice, however, that there is a dupinstruction before these two appear; the incremented value is (of course) not reflected in the return value.

如您所见,指令 6 和 7ixAdd()处理返回前的增量。因此,正如我们所期望的,递减运算符在 return 语句中使用时确实有效果。但是请注意,dup在这两个出现之前有一条指令;增加的值(当然)没有反映在返回值中。

回答by Eddie

Yes, the usual rules apply for post increment even on a return statement. Basically, what it will do at a low level is store the return value into a register, then increment the class member, then return from the method.

是的,即使在 return 语句中,通常的规则也适用于后增量。基本上,它会在低级别做的是将返回值存储到寄存器中,然后增加类成员,然后从方法返回。

You can see this in a later answerto this question that showed the byte code.

您可以在显示字节码的此问题的稍后答案中看到这一点。

回答by starblue

Yes, it does.

是的,它确实。

But don't do that, it is bad style to have side effects in expressions.

但是不要那样做,在表达式中产生副作用是不好的风格。

回答by trilobite

It does return the value before the increment, and then increment _ix. Therefore the first time you call the method ixAdd on an instance of myCounter it will return 1, the second time it will return 2, and so on.

它确实返回增量之前的值,然后增量 _ix。因此,当您第一次在 myCounter 的实例上调用 ixAdd 方法时,它将返回 1,第二次将返回 2,以此类推。