Java String to Integer Analysis

Source: Internet
Author: User

We often use the Integer. valueOf (String str) method. If the String format is incorrect, this method will throw a system exception NumberFormatException.
Here we need to analyze this method. Here, Byte and Short also call the method in Ingeter.
The definition of the Integer class is as follows:
Public static Integer valueOf (String s) throws NumberFormatException
{
Return new Integer (parseInt (s, 10 ));
}
Here, because the parseInt method returns the int type, a constructor is called to generate a new Integer instance.
Here we are concerned about the parseInt method. The code for this method is as follows:
Public static int parseInt (String s, int radix)
Throws NumberFormatException
{
If (s = null ){
Throw new NumberFormatException ("null ");
}

If (radix <Character. MIN_RADIX ){
Throw new NumberFormatException ("radix" + radix +
"Less than Character. MIN_RADIX ");
}

If (radix> Character. MAX_RADIX ){
Throw new NumberFormatException ("radix" + radix +
"Greater than Character. MAX_RADIX ");
}

Int result = 0;
Boolean negative = false;
Int I = 0, max = s. length ();
Int limit;
Int multmin;
Int digit;

If (max> 0 ){
If (s. charAt (0) = '-'){
Negative = true;
Limit = Integer. MIN_VALUE;
I ++;
} Else {
Limit =-Integer. MAX_VALUE;
}

If (I <max ){
Digit = Character. digit (s. charAt (I ++), radix );
If (digit <0 ){
Throw NumberFormatException. forInputString (s );
} Else {
Result =-digit;
}
}
While (I <max ){
// Accumulating negatively avoids surprises near MAX_VALUE
Digit = Character. digit (s. charAt (I ++), radix );
If (digit <0 ){
Throw NumberFormatException. forInputString (s );
}
If (result <multmin ){
Throw NumberFormatException. forInputString (s); Exception 1
}
Result * = radix;
If (result <limit + digit ){
Throw NumberFormatException. forInputString (s); Exception 2
}
Result-= digit;
}
} Else {
Throw NumberFormatException. forInputString (s );
}

If (negative ){
If (I> 1 ){
Return result;
} Else {/* Only got "-"*/
Throw NumberFormatException. forInputString (s );
}
} Else {
Return-result;
}
}

Obviously, the second parameter of this method indicates the base (the most commonly used is decimal, there are sixteen mechanisms, octal, and so on ).
If the string is a null pointer, an exception is thrown directly.
If the base value is less than 2 or greater than 36, an exception is thrown (this usually does not happen, because we use a decimal number at most ).
If it is a Null String, an exception is thrown, that is, the case of max = 0.
Let's focus on the following conversion process:
Here, the static method digit in Character is used. This method is complicated. Here we first describe its function: for a given base, if it is a legal Character (it can be converted to a number ), returns the numeric value. Otherwise,-1 is returned. for example, digit ('3', 10) returns 3, digit ('A', 10) returns-1.
This program looks very simple, but it is not easy to understand. Here we will explain the meanings of several local variables:
Result: Record Return Value
Negative: Symbol
I: String position
S: String Length
Limit: limit
Multmin: it is also a boundary.
Digit: Number of the Current Character
Check whether the first character is '-', and set the symbol sign negative and limit value limit.
Note that limit must be a negative value.
The highest bitwise of processing. Here, the result is saved as a negative value, which can be used for unified processing of positive and negative numbers.
The key is this while loop. The first if statement does not need to be interpreted. It must be an invalid character.
The meaning of the second if statement: if the result is smaller than multmin, what will happen?
Will it always overflow? The result must be> = limit.
Result is less than multmin, result should be at least a multmin-1, followed by result = result * radix = (multmin-1) * radix = multmin * radix-radix
The value must be smaller than limit, where multmin = limit/radix. Note that all values here are negative.
Therefore, if the result is smaller than multmin, it will certainly overflow later.
If no judgment is made here, overflow will be troublesome, and positive numbers will also become negative.
Meaning of the third if statement: It must have not exceeded before, but it may have exceeded with the last digit, so this judgment is also necessary.
Else Is a Null String "".
If it is a negative number, it depends on whether the length is 1. It is just a '-' number.
If it is a positive number, return the opposite number.
Exceptions may be thrown in many places. If you understand the program, you will know that this exception is thrown by the statement. Overflow exceptions are considered here: exception 1 and exception 2.
Ingeter. Max_VALUE = 2147483647
The following two statements throw an exception in different places.
Ingeter. valueOf ("2147483648"); this is thrown in exception 2.
Ingeter. valueOf ("21474836471"); this is thrown in exception 1.

Here we briefly analyze the process of converting String to Ingeter. In fact, the entire Ingeter class is mainly about this method, and both Byte and Short call this method.
Check the Byte code:
Public static byte parseByte (String s, int radix)
Throws NumberFormatException {
Int I = Integer. parseInt (s, radix );
If (I <MIN_VALUE | I> MAX_VALUE)
Throw new NumberFormatException (
"Value out of range. Value: \" "+ s +" \ "Radix:" + radix );
Return (byte) I;
}

After learning about this method, we will no longer be surprised by the exceptions caused by Integer. valueOf (). Especially in JSP, because the parameters are of the String type, they will not change during conversion.

You should know what's going on.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.