Small description of String and basic type conversion in JAVA, and string Conversion in java

Source: Internet
Author: User

Small description of String and basic type conversion in JAVA, and string Conversion in java

We all know that when the String type is converted to the basic type, you can use the static methods of the basic type Integer. parseInt (String str) and Integer. valueOf (String str). The two methods are slightly different:

Integer. valueOf (String str) returns an Integer, while Integer. parseInt (String str) returns the number of int types (because of automatic encapsulation, the return value is basically different ).

The methods for listing the two are as follows:

1.

Public static Integer valueOf (String s) throws NumberFormatException

{
Return Integer. valueOf (parseInt (s, 10 ));
}

2.

Public static int parseInt (String s) throws NumberFormatException

{
Return parseInt (s, 10 );
}

The above two methods use the static method parseInt (String s, int radix) to list this method:

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, len = s. length ();
Int limit =-Integer. MAX_VALUE;
Int multmin;
Int digit;

If (len> 0 ){
Char firstChar = s. charAt (0 );
If (firstChar <'0') {// Possible leading "+" or "-"
If (firstChar = '-'){
Negative = true;
Limit = Integer. MIN_VALUE;
} Else if (firstChar! = '+ ')
Throw NumberFormatException. forInputString (s );

If (len = 1) // Cannot have lone "+" or "-"
Throw NumberFormatException. forInputString (s );
I ++;
}
Multmin = limit/radix;
While (I <len ){
// 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 );
}
Result * = radix;
If (result <limit + digit ){
Throw NumberFormatException. forInputString (s );
}
Result-= digit;
}
} Else {
Throw NumberFormatException. forInputString (s );
}
Return negative? Result:-result;
}

In the public static int parseInt (String s, int radix) method, s is the String to be converted, radix is in hexadecimal format, radix is 2, binary is used, radix is 10, it is in hexadecimal notation, and radix = 36, then it is in hexadecimal notation. The range of radix is 2 ~ 36.

Why is there a 36-digit system? The hexadecimal value is 0 ~ F, and so on, the 17-digit system is 0 ~ G, in hexadecimal notation 0 ~ H, then the 36-digit system is 0 ~ Z.

For example, Integer. parseInt ("asd", 36), the return value is 10*36*36 + 28*36 + 13 = 13981. Note that the results of "asd" and "ASD" are the same, case Insensitive!

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.