简体   繁体   English

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

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

Given the following code, will ixAdd do what you'd expect, ie 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 increment的通常规则是否也适用于return语句。

The key part is that a post increment/decrement happens immediately after 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()之前。

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. 如您所见, ixAdd()指令6和7处理返回前的增量。 Therefore, as we would expect, the decrement operator does indeed have an effect when used in a return statement. 因此,正如我们所期望的那样,递减运算符确实在return语句中使用时有效。 Notice, however, that there is a dup instruction before these two appear; 但是请注意,在这两个出现之前有一个dup指令; the incremented value is (of course) not reflected in the return value. 递增的值(当然)不会反映在返回值中。

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. 基本上,它在低级别执行的操作是将返回值存储到寄存器中,然后递增类成员,然后从方法返回。

You can see this in a later answer to this question that showed the byte code. 您可以在稍后回答显示字节代码的问题时看到这一点。

Yes, it does. 是的,它确实。

But don't do that, it is bad style to have side effects in expressions. 但是不要这样做,在表情中有副作用是不好的风格。

It does return the value before the increment, and then increment _ix. 它确实在递增之前返回值,然后递增_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. 因此,第一次在myCounter实例上调用方法ixAdd时,它将返回1,第二次返回2,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM