陜西省建設(shè)廳小紅書關(guān)鍵詞排名優(yōu)化
介紹
Integer是int類型的包裝類,繼承自Number抽象類,實(shí)現(xiàn)了Comparable接口。提供了一些處理int類型的方法,比如int到String類型的轉(zhuǎn)換方法或String類型到int類型的轉(zhuǎn)換方法,當(dāng)然也包含與其他類型之間的轉(zhuǎn)換方法。
- Comparable提供了比較大小的功能
- Number抽象類主要抽象出了對(duì)數(shù)值類型的轉(zhuǎn)換方法。
public final class Integer extends Number implements Comparable<Integer>
常量&變量
/*** A constant holding the minimum value an {@code int} can* have, -2<sup>31</sup>.*最小值0x80000000*/@Native public static final int MIN_VALUE = 0x80000000;/*** A constant holding the maximum value an {@code int} can* have, 2<sup>31</sup>-1.* 最大值0x7fffffff*/@Native public static final int MAX_VALUE = 0x7fffffff;/*** The {@code Class} instance representing the primitive type* {@code int}.** @since JDK1.1* int的原始類型*/@SuppressWarnings("unchecked")public static final Class<Integer> TYPE = (Class<Integer>) Class.getPrimitiveClass("int");/*** All possible chars for representing a number as a String* 數(shù)字表示為字符串的所有可能字符*/final static char[] digits = {'0' , '1' , '2' , '3' , '4' , '5' ,'6' , '7' , '8' , '9' , 'a' , 'b' ,'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,'o' , 'p' , 'q' , 'r' , 's' , 't' ,'u' , 'v' , 'w' , 'x' , 'y' , 'z'};//使用DigitOnes和DigitTens來確定一個(gè)兩位數(shù)int對(duì)應(yīng)的char//十位數(shù)final static char [] DigitTens = {'0', '0', '0', '0', '0', '0', '0', '0', '0', '0','1', '1', '1', '1', '1', '1', '1', '1', '1', '1','2', '2', '2', '2', '2', '2', '2', '2', '2', '2','3', '3', '3', '3', '3', '3', '3', '3', '3', '3','4', '4', '4', '4', '4', '4', '4', '4', '4', '4','5', '5', '5', '5', '5', '5', '5', '5', '5', '5','6', '6', '6', '6', '6', '6', '6', '6', '6', '6','7', '7', '7', '7', '7', '7', '7', '7', '7', '7','8', '8', '8', '8', '8', '8', '8', '8', '8', '8','9', '9', '9', '9', '9', '9', '9', '9', '9', '9',} ;//個(gè)位數(shù)final static char [] DigitOnes = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9','0', '1', '2', '3', '4', '5', '6', '7', '8', '9',} ;//位數(shù)上限的數(shù)組final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,99999999, 999999999, Integer.MAX_VALUE };/*** The value of the {@code Integer}.** @serial* 存儲(chǔ)的int值*/private final int value;/*** The number of bits used to represent an {@code int} value in two's* complement binary form.** @since 1.5* 占用bit位*/@Native public static final int SIZE = 32;/*** The number of bytes used to represent a {@code int} value in two's* complement binary form.** @since 1.8* 占用字節(jié)數(shù)*/public static final int BYTES = SIZE / Byte.SIZE;/** use serialVersionUID from JDK 1.0.2 for interoperability *///序列化版本號(hào)@Native private static final long serialVersionUID = 1360826667806852920L;
-
value:表示Integer對(duì)應(yīng)的int值。
-
MIN_VALUE:定義一個(gè)常量,表示int類型的最小值,-2^31,@Native 表示被注解的內(nèi)容是原生,不影響java代碼的本身邏輯。
-
MAX_VALUE:定義一個(gè)常量,表示int類型的最大值,2^31-1。
-
TYPE:表示這個(gè)包裝類包裝的是基本類型int。
-
Size:定義常量,用于以二進(jìn)制補(bǔ)碼形式表示int值的位數(shù)。
-
BYTES:定義常量,用于以二進(jìn)制補(bǔ)碼形式表示int值的字節(jié)數(shù)。
-
digits:表示所有可能用來表示數(shù)字的字符,因?yàn)閕nt是支持2-36進(jìn)制,所以需要36個(gè)字符在表示不同的數(shù)字。
-
DigitOnes和DigitTens:使用DigitOnes和DigitTens來確定一個(gè)兩位數(shù)int對(duì)應(yīng)的char。如65,DigitOnes[65]=5,DigitTens[65]=6。
-
sizeTable:主要用來計(jì)算一個(gè)int類型對(duì)應(yīng)字符串的長(zhǎng)度。如下的stringSize方法
構(gòu)造方法
/*** Constructs a newly allocated {@code Integer} object that* represents the specified {@code int} value.** @param value the value to be represented by the* {@code Integer} object.*/public Integer(int value) {this.value = value;}/*** Constructs a newly allocated {@code Integer} object that* represents the {@code int} value indicated by the* {@code String} parameter. The string is converted to an* {@code int} value in exactly the manner used by the* {@code parseInt} method for radix 10.** @param s the {@code String} to be converted to an* {@code Integer}.* @exception NumberFormatException if the {@code String} does not* contain a parsable integer.* @see java.lang.Integer#parseInt(java.lang.String, int)*/public Integer(String s) throws NumberFormatException {this.value = parseInt(s, 10);}
常用方法
toString
將int轉(zhuǎn)成字符串
/*** Returns a {@code String} object representing this* {@code Integer}'s value. The value is converted to signed* decimal representation and returned as a string, exactly as if* the integer value were given as an argument to the {@link* java.lang.Integer#toString(int)} method.** @return a string representation of the value of this object in* base 10.*/public String toString() {return toString(value);}
/*** Returns a {@code String} object representing the* specified integer. The argument is converted to signed decimal* representation and returned as a string, exactly as if the* argument and radix 10 were given as arguments to the {@link* #toString(int, int)} method.** @param i an integer to be converted.* @return a string representation of the argument in base 10.*/public static String toString(int i) {//int的最小值if (i == Integer.MIN_VALUE)return "-2147483648";//調(diào)用stringSize計(jì)算int值對(duì)應(yīng)字符串的長(zhǎng)度,負(fù)數(shù)有一個(gè)符號(hào)位所以要+1int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);//新建一個(gè)臨時(shí)數(shù)組,用來存放int值每一位轉(zhuǎn)成char后的值char[] buf = new char[size];//將int值每一位轉(zhuǎn)成char放到buf中getChars(i, size, buf);return new String(buf, true);}
/*** Returns a string representation of the first argument in the* radix specified by the second argument.** <p>If the radix is smaller than {@code Character.MIN_RADIX}* or larger than {@code Character.MAX_RADIX}, then the radix* {@code 10} is used instead.** <p>If the first argument is negative, the first element of the* result is the ASCII minus character {@code '-'}* ({@code '\u005Cu002D'}). If the first argument is not* negative, no sign character appears in the result.** <p>The remaining characters of the result represent the magnitude* of the first argument. If the magnitude is zero, it is* represented by a single zero character {@code '0'}* ({@code '\u005Cu0030'}); otherwise, the first character of* the representation of the magnitude will not be the zero* character. The following ASCII characters are used as digits:** <blockquote>* {@code 0123456789abcdefghijklmnopqrstuvwxyz}* </blockquote>** These are {@code '\u005Cu0030'} through* {@code '\u005Cu0039'} and {@code '\u005Cu0061'} through* {@code '\u005Cu007A'}. If {@code radix} is* <var>N</var>, then the first <var>N</var> of these characters* are used as radix-<var>N</var> digits in the order shown. Thus,* the digits for hexadecimal (radix 16) are* {@code 0123456789abcdef}. If uppercase letters are* desired, the {@link java.lang.String#toUpperCase()} method may* be called on the result:** <blockquote>* {@code Integer.toString(n, 16).toUpperCase()}* </blockquote>** @param i an integer to be converted to a string.* @param radix the radix to use in the string representation.* @return a string representation of the argument in the specified radix.* @see java.lang.Character#MAX_RADIX* @see java.lang.Character#MIN_RADIX*/public static String toString(int i, int radix) {//當(dāng)基數(shù)小于2或大于36,radix默認(rèn)為10進(jìn)制if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX)radix = 10;/* Use the faster version *///當(dāng)基數(shù)為10時(shí),直接調(diào)用toString方法后返回if (radix == 10) {return toString(i);}//因?yàn)閕nt最大為32位(2進(jìn)制占的位數(shù)),所以只需要33位就可以存儲(chǔ)int+符號(hào)位char buf[] = new char[33];boolean negative = (i < 0);int charPos = 32;//將正數(shù)轉(zhuǎn)換為負(fù)數(shù)if (!negative) {i = -i;}//循環(huán) 當(dāng)負(fù)值i 依然小于 負(fù)值radixwhile (i <= -radix) {//buf[32] = digits[]buf[charPos--] = digits[-(i % radix)];i = i / radix;}buf[charPos] = digits[-i];//i小于0,符號(hào)標(biāo)志位為'-'if (negative) {buf[--charPos] = '-';}return new String(buf, charPos, (33 - charPos));}
stringSize
獲取一個(gè)int值對(duì)應(yīng)字符串的長(zhǎng)度
// Requires positive x//返回位數(shù) 利用sizeTable屬性,可以高效的獲取一個(gè)int值對(duì)應(yīng)字符串你的長(zhǎng)度,不用過多的除法或取模運(yùn)算static int stringSize(int x) {for (int i=0; ; i++)if (x <= sizeTable[i])return i+1;}
getChars
在toString方法中調(diào)用,主要作用是,將int值的每一位轉(zhuǎn)成char后放到buf中。
/*** Places characters representing the integer i into the* character array buf. The characters are placed into* the buffer backwards starting with the least significant* digit at the specified index (exclusive), and working* backwards from there.** Will fail if i == Integer.MIN_VALUE*/static void getChars(int i, int index, char[] buf) {int q, r;//buf數(shù)組的長(zhǎng)度int charPos = index;//符號(hào)標(biāo)志位char sign = 0;//當(dāng)i小于0時(shí),if (i < 0) {//定義符號(hào)位‘-’sign = '-';//將負(fù)值i取反i = -i;}// Generate two digits per iteration//①如果i大于65536(兩個(gè)字節(jié)的長(zhǎng)度)那么就去除i的后兩位while (i >= 65536) {//去除i的后兩位賦值給q 比如i為65536,那么q為655q = i / 100;// really: r = i - (q * 100);//②計(jì)算后兩位的值,如果i為65537,那么r為37,公式 r = 65537 - (655 * 100)r = i - ((q << 6) + (q << 5) + (q << 2));//去除后兩位重新賦值ii = q;//通過DigitOnes和DigitTens獲取r的個(gè)位和十位對(duì)應(yīng)的char。buf [--charPos] = DigitOnes[r];buf [--charPos] = DigitTens[r];}// Fall thru to fast mode for smaller numbers// assert(i <= 65536, i);//經(jīng)過上面循環(huán),i小于等于65536for (;;) {//③就是q = i/10,如果i=655,那么q=65q = (i * 52429) >>> (16+3);//取i的最后一位 r= 655 - (65 * 10) = 5r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...//通過digits數(shù)組獲取對(duì)應(yīng)的charbuf [--charPos] = digits [r];// q=65,并賦值給i,進(jìn)入下一個(gè)循環(huán)i = q;if (i == 0) break;}//符號(hào)標(biāo)志為不為0 即為‘-’if (sign != 0) {//數(shù)組下標(biāo)為0的char為‘-’buf [--charPos] = sign;}}
parseInt
/*** Parses the string argument as a signed integer in the radix* specified by the second argument. The characters in the string* must all be digits of the specified radix (as determined by* whether {@link java.lang.Character#digit(char, int)} returns a* nonnegative value), except that the first character may be an* ASCII minus sign {@code '-'} ({@code '\u005Cu002D'}) to* indicate a negative value or an ASCII plus sign {@code '+'}* ({@code '\u005Cu002B'}) to indicate a positive value. The* resulting integer value is returned.** <p>An exception of type {@code NumberFormatException} is* thrown if any of the following situations occurs:* <ul>* <li>The first argument is {@code null} or is a string of* length zero.** <li>The radix is either smaller than* {@link java.lang.Character#MIN_RADIX} or* larger than {@link java.lang.Character#MAX_RADIX}.** <li>Any character of the string is not a digit of the specified* radix, except that the first character may be a minus sign* {@code '-'} ({@code '\u005Cu002D'}) or plus sign* {@code '+'} ({@code '\u005Cu002B'}) provided that the* string is longer than length 1.** <li>The value represented by the string is not a value of type* {@code int}.* </ul>** <p>Examples:* <blockquote><pre>* parseInt("0", 10) returns 0* parseInt("473", 10) returns 473* parseInt("+42", 10) returns 42* parseInt("-0", 10) returns 0* parseInt("-FF", 16) returns -255* parseInt("1100110", 2) returns 102* parseInt("2147483647", 10) returns 2147483647* parseInt("-2147483648", 10) returns -2147483648* parseInt("2147483648", 10) throws a NumberFormatException* parseInt("99", 8) throws a NumberFormatException* parseInt("Kona", 10) throws a NumberFormatException* parseInt("Kona", 27) returns 411787* </pre></blockquote>** @param s the {@code String} containing the integer* representation to be parsed* @param radix the radix to be used while parsing {@code s}.* @return the integer represented by the string argument in the* specified radix.* @exception NumberFormatException if the {@code String}* does not contain a parsable {@code int}.* 將radix進(jìn)制的String類型整數(shù)轉(zhuǎn)換為int類型。*/public static int parseInt(String s, int radix)throws NumberFormatException{/** WARNING: This method may be invoked early during VM initialization* before IntegerCache is initialized. Care must be taken to not use* the valueOf method.*/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);// 若firstChar < '0' 說明第一個(gè)字符是+或—。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++;}// 這個(gè)變量是為了防止超過最大整數(shù)multmin = limit / radix;while (i < len) {// Accumulating negatively avoids surprises near MAX_VALUE// 獲取進(jìn)制為radix的字符i的整數(shù)int類型digit = Character.digit(s.charAt(i++),radix);if (digit < 0) {throw NumberFormatException.forInputString(s);}// 乘以radix之前先判斷是否越界if (result < multmin) {throw NumberFormatException.forInputString(s);}result *= radix;if (result < limit + digit) {throw NumberFormatException.forInputString(s);}// 這里使用負(fù)數(shù)進(jìn)行計(jì)算,因?yàn)樽钚∝?fù)數(shù)比最大正數(shù)多一個(gè),不然可能出現(xiàn)溢出result -= digit;}} else {throw NumberFormatException.forInputString(s);}return negative ? result : -result;}/*** Parses the string argument as a signed decimal integer. The* characters in the string must all be decimal digits, except* that the first character may be an ASCII minus sign {@code '-'}* ({@code '\u005Cu002D'}) to indicate a negative value or an* ASCII plus sign {@code '+'} ({@code '\u005Cu002B'}) to* indicate a positive value. The resulting integer value is* returned, exactly as if the argument and the radix 10 were* given as arguments to the {@link #parseInt(java.lang.String,* int)} method.** @param s a {@code String} containing the {@code int}* representation to be parsed* @return the integer value represented by the argument in decimal.* @exception NumberFormatException if the string does not contain a* parsable integer.* 默認(rèn)十進(jìn)制*/public static int parseInt(String s) throws NumberFormatException {return parseInt(s,10);}
parseUnsignedInt
/*** Parses the string argument as an unsigned integer in the radix* specified by the second argument. An unsigned integer maps the* values usually associated with negative numbers to positive* numbers larger than {@code MAX_VALUE}.** The characters in the string must all be digits of the* specified radix (as determined by whether {@link* java.lang.Character#digit(char, int)} returns a nonnegative* value), except that the first character may be an ASCII plus* sign {@code '+'} ({@code '\u005Cu002B'}). The resulting* integer value is returned.** <p>An exception of type {@code NumberFormatException} is* thrown if any of the following situations occurs:* <ul>* <li>The first argument is {@code null} or is a string of* length zero.** <li>The radix is either smaller than* {@link java.lang.Character#MIN_RADIX} or* larger than {@link java.lang.Character#MAX_RADIX}.** <li>Any character of the string is not a digit of the specified* radix, except that the first character may be a plus sign* {@code '+'} ({@code '\u005Cu002B'}) provided that the* string is longer than length 1.** <li>The value represented by the string is larger than the* largest unsigned {@code int}, 2<sup>32</sup>-1.** </ul>*** @param s the {@code String} containing the unsigned integer* representation to be parsed* @param radix the radix to be used while parsing {@code s}.* @return the integer represented by the string argument in the* specified radix.* @throws NumberFormatException if the {@code String}* does not contain a parsable {@code int}.* @since 1.8* 將String類型的無符號(hào)數(shù)轉(zhuǎn)換為int類型。*/public static int parseUnsignedInt(String s, int radix)throws NumberFormatException {if (s == null) {throw new NumberFormatException("null");}int len = s.length();if (len > 0) {char firstChar = s.charAt(0);if (firstChar == '-') {throw newNumberFormatException(String.format("Illegal leading minus sign " +"on unsigned string %s.", s));} else {// 這里先判斷String長(zhǎng)度是否小于等于5,這是因?yàn)樽畲笳麛?shù)用36進(jìn)制表示為6位,越界了if (len <= 5 || // Integer.MAX_VALUE in Character.MAX_RADIX is 6 digits// 因?yàn)?0進(jìn)制比較常用,所以這里它專門判斷是不是10進(jìn)制(radix == 10 && len <= 9) ) { // Integer.MAX_VALUE in base 10 is 10 digitsreturn parseInt(s, radix);} else {// 如果無法用parseInt來轉(zhuǎn)換就需要使用長(zhǎng)整型longlong ell = Long.parseLong(s, radix);// 若轉(zhuǎn)換后的long高32位有數(shù)字說明越界了if ((ell & 0xffff_ffff_0000_0000L) == 0) {return (int) ell;} else {throw newNumberFormatException(String.format("String value %s exceeds " +"range of unsigned int.", s));}}}} else {throw NumberFormatException.forInputString(s);}}/*** Parses the string argument as an unsigned decimal integer. The* characters in the string must all be decimal digits, except* that the first character may be an an ASCII plus sign {@code* '+'} ({@code '\u005Cu002B'}). The resulting integer value* is returned, exactly as if the argument and the radix 10 were* given as arguments to the {@link* #parseUnsignedInt(java.lang.String, int)} method.** @param s a {@code String} containing the unsigned {@code int}* representation to be parsed* @return the unsigned integer value represented by the argument in decimal.* @throws NumberFormatException if the string does not contain a* parsable unsigned integer.* @since 1.8*/public static int parseUnsignedInt(String s) throws NumberFormatException {return parseUnsignedInt(s, 10);}
valueOf
/*** Returns an {@code Integer} object holding the value* extracted from the specified {@code String} when parsed* with the radix given by the second argument. The first argument* is interpreted as representing a signed integer in the radix* specified by the second argument, exactly as if the arguments* were given to the {@link #parseInt(java.lang.String, int)}* method. The result is an {@code Integer} object that* represents the integer value specified by the string.** <p>In other words, this method returns an {@code Integer}* object equal to the value of:** <blockquote>* {@code new Integer(Integer.parseInt(s, radix))}* </blockquote>** @param s the string to be parsed.* @param radix the radix to be used in interpreting {@code s}* @return an {@code Integer} object holding the value* represented by the string argument in the specified* radix.* @exception NumberFormatException if the {@code String}* does not contain a parsable {@code int}.* 調(diào)用ParseInt方法將String轉(zhuǎn)換為Integer。*/public static Integer valueOf(String s, int radix) throws NumberFormatException {return Integer.valueOf(parseInt(s,radix));}/*** Returns an {@code Integer} object holding the* value of the specified {@code String}. The argument is* interpreted as representing a signed decimal integer, exactly* as if the argument were given to the {@link* #parseInt(java.lang.String)} method. The result is an* {@code Integer} object that represents the integer value* specified by the string.** <p>In other words, this method returns an {@code Integer}* object equal to the value of:** <blockquote>* {@code new Integer(Integer.parseInt(s))}* </blockquote>** @param s the string to be parsed.* @return an {@code Integer} object holding the value* represented by the string argument.* @exception NumberFormatException if the string cannot be parsed* as an integer.*/public static Integer valueOf(String s) throws NumberFormatException {return Integer.valueOf(parseInt(s, 10));}/*** Returns an {@code Integer} instance representing the specified* {@code int} value. If a new {@code Integer} instance is not* required, this method should generally be used in preference to* the constructor {@link #Integer(int)}, as this method is likely* to yield significantly better space and time performance by* caching frequently requested values.** This method will always cache values in the range -128 to 127,* inclusive, and may cache other values outside of this range.** @param i an {@code int} value.* @return an {@code Integer} instance representing {@code i}.* @since 1.5* 首先判斷緩存里有沒有,如果有就從緩存里面拿,沒有就創(chuàng)建一個(gè)。*/public static Integer valueOf(int i) {if (i >= IntegerCache.low && i <= IntegerCache.high)return IntegerCache.cache[i + (-IntegerCache.low)];return new Integer(i);}
IntegerCache
/*** Cache to support the object identity semantics of autoboxing for values between* -128 and 127 (inclusive) as required by JLS.** The cache is initialized on first usage. The size of the cache* may be controlled by the {@code -XX:AutoBoxCacheMax=<size>} option.* During VM initialization, java.lang.Integer.IntegerCache.high property* may be set and saved in the private system properties in the* sun.misc.VM class.* 緩存靜態(tài)內(nèi)部類 -128 ,127*/private static class IntegerCache {static final int low = -128;static final int high;static final Integer cache[];static {// high value may be configured by propertyint h = 127;// 這個(gè)是啟動(dòng)虛擬機(jī)的時(shí)候帶的參數(shù),可以自行設(shè)置表示緩存的最大整數(shù)String integerCacheHighPropValue =sun.misc.VM.getSavedProperty("java.lang.Integer.IntegerCache.high");if (integerCacheHighPropValue != null) {try {int i = parseInt(integerCacheHighPropValue);i = Math.max(i, 127);// Maximum array size is Integer.MAX_VALUE// 緩存的最大整數(shù)h = Math.min(i, Integer.MAX_VALUE - (-low) -1);} catch( NumberFormatException nfe) {// If the property cannot be parsed into an int, ignore it.}}high = h;cache = new Integer[(high - low) + 1];int j = low;for(int k = 0; k < cache.length; k++)cache[k] = new Integer(j++);// range [-128, 127] must be interned (JLS7 5.1.7)assert IntegerCache.high >= 127;}private IntegerCache() {}}
decode
/*** Decodes a {@code String} into an {@code Integer}.* Accepts decimal, hexadecimal, and octal numbers given* by the following grammar:** <blockquote>* <dl>* <dt><i>DecodableString:</i>* <dd><i>Sign<sub>opt</sub> DecimalNumeral</i>* <dd><i>Sign<sub>opt</sub></i> {@code 0x} <i>HexDigits</i>* <dd><i>Sign<sub>opt</sub></i> {@code 0X} <i>HexDigits</i>* <dd><i>Sign<sub>opt</sub></i> {@code #} <i>HexDigits</i>* <dd><i>Sign<sub>opt</sub></i> {@code 0} <i>OctalDigits</i>** <dt><i>Sign:</i>* <dd>{@code -}* <dd>{@code +}* </dl>* </blockquote>** <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>* are as defined in section 3.10.1 of* <cite>The Java™ Language Specification</cite>,* except that underscores are not accepted between digits.** <p>The sequence of characters following an optional* sign and/or radix specifier ("{@code 0x}", "{@code 0X}",* "{@code #}", or leading zero) is parsed as by the {@code* Integer.parseInt} method with the indicated radix (10, 16, or* 8). This sequence of characters must represent a positive* value or a {@link NumberFormatException} will be thrown. The* result is negated if first character of the specified {@code* String} is the minus sign. No whitespace characters are* permitted in the {@code String}.** @param nm the {@code String} to decode.* @return an {@code Integer} object holding the {@code int}* value represented by {@code nm}* @exception NumberFormatException if the {@code String} does not* contain a parsable integer.* @see java.lang.Integer#parseInt(java.lang.String, int)* 將String類型的nm解碼為Integer類型*/public static Integer decode(String nm) throws NumberFormatException {int radix = 10;int index = 0;boolean negative = false;Integer result;if (nm.isEmpty())throw new NumberFormatException("Zero length string");char firstChar = nm.charAt(0);// Handle sign, if present// 首先判斷是否有符號(hào)if (firstChar == '-') {negative = true;index++;} else if (firstChar == '+')index++;// Handle radix specifier, if present// 查看字符串表示的整數(shù)的進(jìn)制// 是否是16進(jìn)制if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {index += 2;radix = 16;}// 是否是16進(jìn)制else if (nm.startsWith("#", index)) {index ++;radix = 16;}// 是否是8進(jìn)制else if (nm.startsWith("0", index) && nm.length() > 1 + index) {index ++;radix = 8;}// 判斷符號(hào)是否寫錯(cuò)地方了if (nm.startsWith("-", index) || nm.startsWith("+", index))throw new NumberFormatException("Sign character in wrong position");try {// 將相應(yīng)進(jìn)制的字符串轉(zhuǎn)換為對(duì)應(yīng)的Integer類型// 這里如果是最小負(fù)數(shù)會(huì)出錯(cuò)進(jìn)入到下面的catch語句中處理// 這里有點(diǎn)操作麻煩了,如果是我就會(huì)在這里將nm的符號(hào)一起傳入result = Integer.valueOf(nm.substring(index), radix);// 將符號(hào)賦值給resultresult = negative ? Integer.valueOf(-result.intValue()) : result;} catch (NumberFormatException e) {// If number is Integer.MIN_VALUE, we'll end up here. The next line// handles this case, and causes any genuine format error to be// rethrown.String constant = negative ? ("-" + nm.substring(index)): nm.substring(index);result = Integer.valueOf(constant, radix);}return result;}
github:Integer源碼
如文章有問題請(qǐng)留言,謝謝~