Reputation: 777
I am quite confused if there any difference the two below. I think they are doing the same thing but when I compile it seems like they are different. Thank you in advance.
if(hash[s.charAt(leftIdx)]>=0) {
hash[s.charAt(leftIdx)]++;
// do other things
}
if(hash[s.charAt(leftIdx)]++>=0) {
// do other things
}
Upvotes: 0
Views: 56
Reputation: 2734
Refactoring to remove the postfix operators, the first one is equivalent to this:
if(hash[s.charAt(leftIdx)] >= 0) {
hash[s.charAt(leftIdx)] += 1;
// do other things
}
The second (assuming your hash
members are integers) is equivalent to this:
int tmp = hash[s.charAt(leftIdx)];
hash[s.charAt(leftIdx)] += 1;
if(tmp >= 0) {
// do other things
}
The postfix ++
operator returns the pre-incremented value and then increments it as a side-effect. So your if
in the 2nd example is using the pre-incremented value in its condition (represented by tmp
above).
Upvotes: 2