Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Integer.parseInt("1")++ work in Java?

Tags:

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?

like image 562
ssloan Avatar asked Dec 16 '11 15:12

ssloan


People also ask

How does integer parseInt work in Java?

parseInt(String s) − This returns an integer (decimal only). parseInt(int i) − This returns an integer, given a string representation of decimal, binary, octal, or hexadecimal (radix equals 10, 2, 8, or 16 respectively) numbers as input.

When parseInt () method can be used in Java?

There are two variants of this method: parseInt(String s): This function parses the string argument as a signed decimal integer. parseInt(String s, int radix): This function parses the string argument as a signed integer in the radix specified by the second argument.

What does parseInt return if not an integer?

If parseInt encounters a character that is not a numeral in the specified radix , it ignores it and all succeeding characters and returns the integer value parsed up to that point.

Does parseInt have a limit?

Your number is too large to fit in an int which is 32 bits and only has a range of -2,147,483,648 to 2,147,483,647. Try Long. parseLong instead.


4 Answers

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 is Integer.parseInt(suffix), has no fixed memory address to be updated at all.

Intuitively, Integer.parseInt(suffix)++ is roughly equivalent to Integer.parseInt(suffix) = Integer.parseInt(suffix) + 1. But Integer.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 to 32 (neither to Integer.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)++, write Integer.parseInt(suffix)+1.

* Or, as it is most commonly called, an l-value, or an address value.

like image 170
brandizzi Avatar answered Oct 14 '22 09:10

brandizzi


++ requires an lvalue (an assignable value).

Integer.parseInt(suffix) is not an lvalue.

Note that i++ is not the same as i+1.

like image 24
Klas Lindbäck Avatar answered Oct 14 '22 11:10

Klas Lindbäck


Writing i++ is a shortcut for i=i+1; if you were to 'read it in english' you'd read it as "i becomes current value of i plus one"

which is why 3++ doesn't make sense you can't really say 3 = 3+1 (read as 3 becomes current value of 3 plus one) :-)

like image 27
Ahmed Masud Avatar answered Oct 14 '22 09:10

Ahmed Masud


The int is an rvalue. Since it isn't bound to a variable you cannot use post-incrementation.

like image 25
Filip Roséen - refp Avatar answered Oct 14 '22 11:10

Filip Roséen - refp