방법 공공 정적 INT의에서는 parseInt은 (문자열 STR) 및 공공 정적의 INT의에서는 parseInt (문자열 STR, INT의 redix)어떻게있는 Integer.parseInt가
어떻게 작동합니까 작동합니까?
& 그들 사이의 차이점은 무엇입니까?
방법 공공 정적 INT의에서는 parseInt은 (문자열 STR) 및 공공 정적의 INT의에서는 parseInt (문자열 STR, INT의 redix)어떻게있는 Integer.parseInt가
어떻게 작동합니까 작동합니까?
& 그들 사이의 차이점은 무엇입니까?
기본적으로 동일한 기능입니다. parseInt(String str)
은 밑이 10 인 것으로 가정합니다 (문자열이 0x
또는 0
으로 시작하지 않는 한). parseInt(String str, int radix)
은 지정된 기준을 사용합니다. 나는 코드를 보지 않았지만 첫 번째 코드는 parseInt(str, 10)
(단지 두 개의 특수한 경우를 제외하고는 16
과 8
을 사용)을 호출 할 것입니다.
오, 자바, 오픈 소스가 얼마나 좋은가.
/**
* Parses the specified string as a signed decimal integer value. The ASCII
* character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of an integer value.
* @return the primitive integer value represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value.
*/
public static int parseInt(String string) throws NumberFormatException {
return parseInt(string, 10);
}
과 기수를 사용한 : JDK6의 정수에서
/**
* Parses the specified string as a signed integer value using the specified
* radix. The ASCII character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of an integer value.
* @param radix
* the radix to use when parsing.
* @return the primitive integer value represented by {@code string} using
* {@code radix}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value,
* or {@code radix < Character.MIN_RADIX ||
* radix > Character.MAX_RADIX}.
*/
public static int parseInt(String string, int radix) throws NumberFormatException {
if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) {
throw new NumberFormatException("Invalid radix: " + radix);
}
if (string == null) {
throw invalidInt(string);
}
int length = string.length(), i = 0;
if (length == 0) {
throw invalidInt(string);
}
boolean negative = string.charAt(i) == '-';
if (negative && ++i == length) {
throw invalidInt(string);
}
return parse(string, i, radix, negative);
}
이러한 방법의 소스 코드를 읽을 수 있습니다. – RanRag