简体   繁体   中英

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.

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 . It returns the original value which is 13 after this call.


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-- ? Why does it need - currentPage=currentPage-1; in this scenario?

In your return statement, it uses currentPage-- which causes the decrement after the return. You'd want --currentPage to do the decrement before the return. 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.


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:

*12
13

If we replace currentPage-- with --currentPage , we receive:

*12
12

as expected.

But , don't you think it would be better to simply use currentPage - 1 instead? There is no reason to reassign currentPage in this scenario (remember, such a reassignment will not be visible outside the scope of the method).


The prefix decrement operator is covered in §15.15.2 of the JLS. Notice the sentence:

The value of the prefix decrement expression is the value of the variable after the new value is stored.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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