Tiny
Tiny

Reputation: 27899

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 (i.e 13) minus 1 (i.e 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?

Upvotes: 1

Views: 2771

Answers (2)

arshajii
arshajii

Reputation: 129507

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.

Upvotes: 4

corsiKa
corsiKa

Reputation: 82579

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;

}

Upvotes: 5

Related Questions