JDK Source Learning reading the analysis of parseint method in-integer class

Source: Internet
Author: User

Method Prototypes:

public static int parseint (String s,int radix);

Input: s represents the string to be converted; Radix represents the integer that needs to be converted into a binary number;

Output: Returns a 32-bit integer.

Algorithm Flowchart:

Code implementations in the JDK:

1         /**2 * String converted to integer3  * @params to convert the string4  * @paramRadix in the system5  * @return6  */7  Public Static intparseint (String S,intRadix) {8     //Boundary Value Processing9     if(s==NULL)Ten         Throw NewNumberFormatException ("null"); One     if(radix<Character.min_radix) { A         Throw NewNumberFormatException ("Radix" +radix+ "less than Character.min_radix"); -     } -     if(radix>Character.max_radix) { the         Throw NewNumberFormatException ("Radix" +radix+ "greater than Character.max_radix"); -     } -      -     intResult=0; +      -     //symbol bit judgment +     BooleanNegative=false; A      at     //string offset pointer -     intI=0; -      -     intDigit; -      -     intmax=s.length (); in      -     //Maximum boundary value to     intlimit; +      -     //maximum boundary value shift right one the     intmultmin; *      $     if(max>0){Panax Notoginseng         //Working with symbols -         if(S.charat (0) = = '-'){ theNegative=true; +             //boundary value is 0x80000000 Alimit=Integer.min_value; thei++; +         } -         Else{ $             //boundary value is -0x7fffffff $limit=-Integer.max_value; -         } -          themultmin=limit/Radix; -         if(i<max) {WuyiDigit=character.digit (S.charat (i++), radix); the             if(digit<0){ -                 Thrownumberformatexception.forinputstring (s); Wu             } -             Else{ Aboutresult=-Digit; $             } -         } -          while(i<max) { -             //converts a character to a corresponding binary integer ADigit=character.digit (S.charat (i++), radix); +             if(digit<0){ the                 Thrownumberformatexception.forinputstring (s); -             } $              the             if(result<multmin) { the                 Thrownumberformatexception.forinputstring (s); the             } theresult*=Radix; -             //Result-digit<limit in             if(result<limit+Digit) { the                 Thrownumberformatexception.forinputstring (s); the             } Aboutresult-=Digit; the         } the     } the     Else{ +         Thrownumberformatexception.forinputstring (s); -     } the     if(negative) {Bayi         if(i>1){ the             returnresult; the         } -         Else{ -             Thrownumberformatexception.forinputstring (s); the         } the     } the     Else{ the         return-result; -     } the}

Key points:

    1. The boundary value of positive number is 1 to 0x7fffffff; the boundary value of negative number is 1 to 0x80000000;
    2. The code handles all data as negative numbers (positive numbers) and finally handles symbolic problems;
    3. This variable is multmin in the method in order to Result*=radix in the loop without crossing the bounds;

JDK Source Learning Read the analysis of parseint method in-integer class (GO)

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.