May P.
Originally posted by May Pat:
Thanks a lot, Valentine, for the great explaination.
I found that Byte, Short, Integer and Long all have parseXXX(String s, int radix) method. I tested with a String and found that they return the same output.
I try different values of the String and these are what I think happen:
1. parseXXX() method checks for the range of the String at runtime. If the string contains integer value greater than XXX.MAX_VALUE or less than XXX.MIN_VALUE, NumberFormatException is thrown.
2. String s must contains only digits 0-9. And if the digit is not in an acceptable range of the radix, NumberFormatException is thrown at runtime. i.e. with the above example, the exception is thrown if the radix is 2 instead of 8 because base 2 contains digits 0 and 1 only.
Am I right?
An exception of type NumberFormatException is thrown if any of the following situations occurs:
The first argument is null or is a string of length zero.
The radix is either smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX.
Any character of the string is not a digit of the specified radix, except that the first character may be a minus sign '-' ('\u002D') provided that the string is longer than length 1.
The value represented by the string is not a value of type int.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Check the API documentation again -- each of those methods has a different return type.
And try changing "12" to "1200" or "120000".