简体   繁体   English

带增量的Java return语句-一般行为是什么?

[英]Java return statement with increment - what is the general behavior?

I just learned that if a return statement contains an increment operation, the return will execute first and the value will be returned before it is incremented. 我刚刚了解到,如果return语句包含一个递增操作,则该返回将首先执行,并且该值将在递增之前返回。 If I increment first in a separate statement, and then return, it works as expected. 如果我先在单独的语句中递增,然后返回,则它将按预期工作。

private static int incrementIntV1(int a)
{
   return a++;
}

private static int incrementIntV2(int a)
{
    a++;
    return a;
}

public static void main(String[] args)
{
    int b = 6;

    System.out.println("Increment in return: " + incrementIntV1(b));
    System.out.println("Increment first, then return: " + incrementIntV2(b));
    System.out.println("Increment with addZero: " + incrementAddZero(b));
}   

What is happening with return that makes it sometimes evaluate the whole expression and sometimes not? return发生了什么,使它有时对整个表达式求值而有时不求值? Is this something special about how the increment operation happens? 这是关于增量操作如何发生的特殊之处吗?

If I try: 如果我尝试:

private static int incrementAddZero(int a)
{
    return a++ + addZero();
}

private static int addZero()
{
    System.out.print("addZero executes");
    return 0;
}

The increment operation still doesn't happen, but I know that the addZero method runs because of the print statement. 增量操作仍然没有发生,但是我知道addZero方法由于print语句而运行。 Why doesn't it increment before the return but it does execute the addZero method before the return? 为什么它在返回之前不增加,但是在返回之前确实执行addZero方法?

Hope his makes sense. 希望他有道理。 Thank you much. 非常感谢

The behavior you encountered has nothing to do with the return statement. 您遇到的行为与return语句无关。 It's the behavior of the post increment operator. 这是后递增运算符的行为。 The expression a++ increments a but returns the previous value of a . 表达a++增量a但返回的先前值a

return a++;

is equivalent to : 等效于:

int x = a++;
return x;

similary, 相似点

return a++ + addZero();

is equivalent to : 等效于:

int x = a++ + addZero();
return x;

or to : 或者 :

int x = a++;
return x + addZero();

a++ returns the value of a before it was incremented. a++在递增a之前返回其值。

What is happening with return that makes it sometimes evaluate the whole expression and sometimes not? return发生了什么,使它有时对整个表达式求值而有时不求值?

return always evaluates the whole expression being returned (unless the return statement contains some short circuited operator such as && and || , but that's not the case here), but the evaluation of the post increment operator a++ returns the old value of a . return的结果总是返回整个表达式(除非return语句包含一些短路操作,如&&|| ,但这里并非如此),但后增量运算符的评价a++回报的旧值a

From the java doc : java doc

The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value . 唯一的区别是,前缀版本(++ result)的值为增量值,而后缀版本(result ++)的值为原始值 If you are just performing a simple increment/decrement, it doesn't really matter which version you choose. 如果您只是执行简单的增量/减量,则选择哪个版本都没有关系。 But if you use this operator in part of a larger expression, the one that you choose may make a significant difference. 但是,如果在较大表达式的一部分中使用此运算符,则选择的运算符可能会产生很大的不同。

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

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