I ran the following code in my Android app
Integer.parseInt("+91");
In Android 5.0 (Lollipop), it did not throw any exception as +91
is an integer. But in Android 4.4.x (KitKat) and lower versions it throws:
NumberFormatException : Invalid Int : "+91"
How is the version of Android causing this difference?
The NumberFormatException is an unchecked exception in Java that occurs when an attempt is made to convert a string with an incorrect format to a numeric value. Therefore, this exception is thrown when it is not possible to convert a string to a numeric type (e.g. int, float).
parseInt() to Convert a String to an Integer. This method returns the string as a primitive type int. If the string does not contain a valid integer then it will throw a NumberFormatException.
parseInt method as a parameter. The method throws an error if the string cannot be parsed into an integer. Note, that the code within the catch block is executed only if an exception is thrown.
The NumberFormatException occurs when an attempt is made to convert a string with improper format into a numeric value. That means, when it is not possible to convert a string in any numeric type (float, int, etc), this exception is thrown. It is a Runtime Exception (Unchecked Exception) in Java. It is a subclass of IllegalArgumentException class.
Java Integer parseInt () Method. 1 1. Java Integer parseInt (String s) Method. This method parses the String argument as a signed decimal integer object. The characters in the string ... 2 2. Java Integer parseInt (String s, int radix) Method. 3 3. Java Integer parseInt (CharSequence s, int beginText, int endText, int radix)
Differences between Integer.parseInt() and Integer.valueOf() Integer.valueOf() returns an Integer object while Integer.parseInt() returns a primitive int. Both String and integer can be passed a parameter to Integer.valueOf() whereas only a String can be passed as parameter to Integer.parseInt().
KitKat did introduce some java 7 features into Android sdk 19, but not the new parseInt. Lower versions use an earlier implementation of parseInt (Java 6's version) so they will obviously fail as well. The difference between parseInt implementations : Java 6 parseInt documentation vs Java 7 parseInt documentation
Support for explicit +
was added in this commit:
Support explicit + in Byte, Short, Integer, Long. Bug: 5239391 Change-Id: I2b25228815d70d570d537db0ed9b5b759f25b5a3
which has been included starting with android-5.0.0_r1
. If you have fetched the Git repository, you can verify with:
git tag --contains 6b40837ee3a023bba698c38fd6d6e46ae0065a55
which gives you
android-5.0.0_r1 android-5.0.0_r2 android-5.0.0_r3 ...
Even though documentation can give insights into why the change was made (to achieve Java 7 behavior as other answers point out), analyzing the history of the source code gives the most accurate answer to when the behavior changed, since documentation does not necessarily match implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With