简体   繁体   English

Java的return语句中的减量(或增量)运算符

[英]Decrement (or increment) operator in return statement in Java

I was implementing pagination in my web application (using Spring and Hibernate) where I needed the stuff something like the following. 我在Web应用程序(使用Spring和Hibernate)中实现分页,需要类似以下内容。

public static int deleteSingle(long totalRows, long pageSize, int currentPage)
{
   return totalRows==currentPage*pageSize-pageSize ? currentPage-- : currentPage;
}

Suppose, I invoke this method from somewhere as follows. 假设我从如下位置调用此方法。

deleteSingle(24, 2, 13);

With these arguments, the condition is satisfied and the value of the variable currentPage (ie 13) minus 1 (ie 12) should be returned but it doesn't decrement the value of currentPage . 有了这些参数,就可以满足条件,并且应该返回变量currentPage (即13)减去1(即12)的值,但不会减小currentPage的值。 It returns the original value which is 13 after this call. 在此调用之后,它将返回原始值13。


I had to change the method like the following for it to work as expected. 我必须更改以下方法才能使其按预期工作。

public static int deleteSingle(long totalRows, long pageSize, int currentPage)
{
    if(totalRows==currentPage*pageSize-pageSize)
    {
        currentPage=currentPage-1;   //<-------
        return currentPage;          //<-------
    }
    else
    {
        return currentPage;
    }
}

So why doesn't it decrement the value by 1 with the decrement operator - currentPage-- ? 那么为什么不使用减数运算符currentPage--将值减1? Why does it need - currentPage=currentPage-1; 为什么需要currentPage=currentPage-1; in this scenario? 在这种情况下?

In your return statement, it uses currentPage-- which causes the decrement after the return. 在您的return语句中,它使用currentPage--在返回之后导致递减。 You'd want --currentPage to do the decrement before the return. 您希望--currentPage在返回之前进行递减。 Personally, with a complicated statement like that, you probably want to break it out anyway for readability's sake, but that's a matter of preference. 就个人而言,使用诸如此类的复杂语句,您可能还是希望出于可读性的考虑而将其分解,但这是优先考虑的问题。

(Technically, it decrements after it's read. There's nothing special about it being a return statement that changes when it decremements.) (从技术上讲,它在读取后会递减。当它递减时,返回值会发生变化,这没有什么特别的。)

If it were up to my, my taste would be to do this: 如果由我决定,我的爱好是这样做:

public static int deleteSingle(long totalRows, long pageSize, int currentPage)
{
    if(totalRows==currentPage*pageSize-pageSize)
    {
        currentPage--;
    }
    return currentPage;

}

Note that x-- decrements x after using its value, you probably want --currentPage , which would decrement the variable before using its value. 请注意, x-- 使用x的值递减x ,您可能需要--currentPage ,它将使用变量的值之前递减该变量。


To see this, consider: 要看到这一点,请考虑:

public static int deleteSingle(long totalRows, long pageSize, int currentPage) {
    try {
        return totalRows == currentPage * pageSize - pageSize ? currentPage--
                    : currentPage;
    } finally {
        System.out.println("*" + currentPage);  // value after return
    }
}

Calling deleteSingle(24, 2, 13) prints: 调用deleteSingle(24, 2, 13)打印:

*12
13

If we replace currentPage-- with --currentPage , we receive: 如果将currentPage--替换为--currentPage--currentPage收到:

*12
12

as expected. 如预期的那样。

But , don't you think it would be better to simply use currentPage - 1 instead? 但是 ,您是否认为仅使用currentPage - 1会更好? There is no reason to reassign currentPage in this scenario (remember, such a reassignment will not be visible outside the scope of the method). 在这种情况下,没有理由重新分配 currentPage (请记住,这种重新分配在方法范围之外是不可见的)。


The prefix decrement operator is covered in §15.15.2 of the JLS. 前缀减量运算符在JLS的第15.15.2节中介绍。 Notice the sentence: 注意以下句子:

The value of the prefix decrement expression is the value of the variable after the new value is stored. 前缀减量表达式的值是存储新值之后的变量的值。

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

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