I’ve got the following line of code:
suffix = suffix.isEmpty() ? "1" : Integer.toString(Integer.parseInt(suffix)+1);
in a block where suffix has already been declared as an empty String (""). The block is looking for duplicate file names and adding a number on to any duplicates so they don’t have the same name any more.
The line of code above compiles fine, but if I change it to this,
suffix = suffix.isEmpty() ? "1" : Integer.toString(Integer.parseInt(suffix)++);
I get Invalid argument to operation ++/--. Since Integer.parseInt() returns and int, why can’t I use the ++ operator?
The
++operator should update the value of its argument, so the argument should have a fixed position in memory to be updated. For this reason, the argument should be a variable*. In this case, the argument isInteger.parseInt(suffix), has no fixed memory address to be updated at all.Intuitively,
Integer.parseInt(suffix)++is roughly equivalent toInteger.parseInt(suffix) = Integer.parseInt(suffix) + 1. ButInteger.parseInt(suffix)is just an integer value, not associated to a fixed position in memory, so the code above is almost the same thing of, let us say,32 = 32 + 1. Since you cannot assign a new value to32(neither toInteger.parseInt(suffix)) then there is no sense in supporting the++operator.The good news is that this does not cause any problems at all! Instead of
Integer.parseInt(suffix)++, writeInteger.parseInt(suffix)+1.* Or, as it is most commonly called, an l-value, or an address value.