String Convert to Integer Source Code Analysis

xiaoxiao2021-03-06  80

We often use the method of Integer.Valueof (String Str), if the string format is wrong, this method will throw a system exception NumberFormatexception Here we want to analyze this method, where Byte, Short also calls the method in INGETER. The definition in the Integer class is as follows: public static integer valueof (String s) throws numberXception {return new integer (Parseint (s, 10));} This is called because of the INT type returned by the PARSEINT method, which calls a constructor generation A new INTEGER instance. He cares about the PARSEINT method. The method code is as follows: public static int parseint (String s, int Radix) throws Numberformatexception {if (s == null) {throw new numberXception ("null);" null "); }

IF (Radix

IF (Radix> Character.max_Radix) {throw new numberXception ("Radix" Radix "Greater Than Character.max_Radix");}

Int results = 0; boolean negative = false; int i = 0, max = s.Length (); int it; int Multmin; int digit;

IF (MAX> 0) {if (s.Charat (0) == '-') {NEGATIVE = true; Limit = Integer.min_Value; i ;} else {limited = -integer.max_value;}

IF (i 1) {return result;} else {/ * only got "-" * / throw number {return (s);}} else {return-Result; }

Obviously, the second parameter of the method is a base (most commonly used is the ten into the decimal, and there are sixteen mechanisms, octal, etc.). If the string is an empty pointer, the exception is thrown directly. If the foundation is less than 2 or greater than 36 If you throw an exception (this general will generally don't appear, because we use the most to be decimal). If it is an empty string, it also throws an exception, which is Max = 0. Let's pay attention to the following Conversion process: This method is used here, which is more complicated. This method is more complicated. He first explains its function: for a given base, if it is a legal character (can be converted to a number), return this numeric value, otherwise returns -1. For example, DIGIT ('3', 10) returns to 3, DIGIT ('A', 10) Returns -1. This program looks simple, in fact, it is really not easy to understand, here is a few local variables Meaning: Result: Record Return Value NEGATIVE: Symbol Sign i: String Location S: String Length Limit: Boundary Multmin: It is also a boundary DIGIT: The number of current characters is first indicated to see if the first character is '-' , Set symbol markers NEGATIVE and limit value Limit. Notice that Limit must be a negative value. Handle the highest bit, where result saves a negative value, so you can unify the positive and negative numbers. The key is this while loop, An IF is not explained, it is definitely because of illegal characters. The meaning of the second IF statement: What results will result in if the result is less than Multmin? Is it possible to overflow? If it doesn't overflow, it is necessary to say the result must be> = Limit.Result is less than Multmin, Result should at least be bitmin-1, behind RESULT = Result * Radix = (Multmin-1) * Radix = Multmin * Radix-radix This value is less than Limit, where Multmin = limit / radix, note here They are negative. So if the result is not intended, if the result is less than Multmin, it will overflow. If there is no judgment here, the overflow is troublesome, and the positive number will also be negative. The meaning of the third IF statement: in this The statement has not been overflow before, but it is possible to add the last digit to overflow, so this judgment is also necessary. The following is better understood, Else is a null string "". If it is negative, Whether the length is 1, it is just a '-' situation. If it is a positive number, return to the opposite number. There are many places to throw an exception, as long as you understand the program, you know this abnormality is that statement. Throwing, here considering overflow exception: exception 1 and abnormal 2.inger.max_value = 2147483647 Two statements throw an abnormality in different places. INGETER.VALUEOF ("2147483648"); this is thrown in an exception 2; "21474836471"); this is thrown at an exception 1. This is a simple analysis. String is converted to the process of Ingeeter. In fact, the entire INGETER class is mainly this method. Byte and short are calling this method. Take a look at Byte's code: public static Byte ParsebyTe (String s, int Radix) THROWS NUMBERFORMATEXCEPTION {INT I = Integer.Parseint (S, RADIX); if (i max_value) throw new number = "value out of range. value: /" " s " / "RADIX:" radix); Return (Byte) I;

After understanding this method, you will never feel unexpectedly for Integer.Valueof (), especially in JSP, because the parameters are String type, and there is different when the conversion is moving.

Often, you should know how to take it.

转载请注明原文地址:https://www.9cbs.com/read-120697.html

New Post(0)