When to use post increment and pre increment in Java [duplicate]
Asked Answered
A

2

6

I understand that there are a number of questions on this topic on StackOverflow. But I am still slightly confused and unsure of when to use the operations. I am going through old tests in studying for my exam. One of the methods returns the number of classrooms that are handicapped accessible and are available. I wrote the counter method but am not sure if I am supposed to pre-increment or post increment the counter. I am confused as to how it works with return statement in methods. I still do not get what value the method will return below. The other questions do not show return values in methods and thus I am confused as to practically how it works. Here is the code:

  public int howManyHandi()
{
    int counter= 0;
    for (int i = 0; i < _clsrms.length; i++){
        if (_clsrms[i].handicappedSuitable() && _clsrms[i].isAvailable()){
            ++counter;
        }
    }
    return counter;
}  
Annunciata answered 14/2, 2016 at 10:30 Comment(0)
L
15

PRE-increment is used when you want to use the incremented value of the variable in that expression., whereas POST-increment uses the original value before incrementing it.

Whenever your code encounters a PRE-increment, it increments the value of that variable in the memory, then load that value and continues reading the expression.

POST-increment does the opposite, it loads that value of that variable in the memory, then increments the value and continues reading the expression.

To make it more clear, consider this

int i = counter++;

is equivalent to

int i = counter;
counter = counter + 1;

WHEREAS

int i = ++counter;

is equivalent to

counter = counter + 1;
int i = counter;

EDIT: My StackOverflow comments arent working, so I'll just edit it here.

What I'm saying it, it only matters when you use that value in an expression.

sum = 0
counter = 0;
sum = (++counter)+(++counter)+(counter++)

evaluates as

sum = 0
counter = 0
//For first ++counter
counter = counter + 1
sum = counter

//For second ++counter
counter = counter + 1
sum = sum + counter

//For first counter++
sum = sum + counter
counter = counter + 1
Leaflet answered 14/2, 2016 at 10:34 Comment(2)
So you are saying that it only really makes a difference when we are assigning the incremented variable to another variable?Annunciata
I hope it is ok I copied this into my notes to help me remember.Annunciata
A
3

In your example it doesn't matter, since you do nothing with the value returned by ++counter.

The only time it makes a difference is when you are using the value returned by the post/pre-increment operator.

For example, if you had a return counter++; or a return ++counter; statement, your method would return a different result based on which operator you used.

Amphibology answered 14/2, 2016 at 10:31 Comment(4)
But let us assume we will be using the value returned by the method somewhere else.Annunciata
@Annunciata it doesn't matter. Both ++counter and counter++ increment counter. Since your method returns counter, it doesn't matter which of the two operators you used.Amphibology
just to clarify, if we assign the value of this method to another variable, such as in a driver class and we want to print the number of classes that are available, it won't make a difference? The variable will be assigned the value of counter?Annunciata
@Annunciata if you mean something like int count = something.howManyHandi();, then yes, it doesn't matter whether you use counter++ or ++counter within howManyHandi. On the other hand, if anywhere in your code you have int variable = counter++; or int variable = ++counter;, the value of variable will be different based on which increment operator you use.Amphibology

© 2022 - 2024 — McMap. All rights reserved.