integer.java
来自「gcc3.2.1源代码」· Java 代码 · 共 593 行 · 第 1/2 页
JAVA
593 行
{ buffer[--i] = Character.forDigit(-(num + radix) % radix, radix); num = -(num / radix); } } else isNeg = false; do { buffer[--i] = Character.forDigit(num % radix, radix); num /= radix; } while (num > 0); if (isNeg) buffer[--i] = '-'; return String.valueOf(buffer, i, 33-i); } /** * Creates a new <code>Integer</code> object using the <code>String</code>, * assuming a radix of 10. * @param s the <code>String</code> to convert. * @return the new <code>Integer</code>. * @see #Integer(java.lang.String) * @see #parseInt(java.lang.String) * @exception NumberFormatException thrown if the <code>String</code> * cannot be parsed as an <code>int</code>. */ public static Integer valueOf(String s) throws NumberFormatException { return new Integer(parseInt(s)); } /** * Creates a new <code>Integer</code> object using the <code>String</code> * and specified radix (base). * @param s the <code>String</code> to convert. * @param radix the radix (base) to convert with. * @return the new <code>Integer</code>. * @see #parseInt(java.lang.String,int) * @exception NumberFormatException thrown if the <code>String</code> * cannot be parsed as an <code>int</code>. */ public static Integer valueOf(String s, int radix) throws NumberFormatException { return new Integer(parseInt(s, radix)); } /** * Converts the specified <code>String</code> into an <code>int</code>. * This function assumes a radix of 10. * * @param s the <code>String</code> to convert * @return the <code>int</code> value of the <code>String</code> * argument. * @exception NumberFormatException thrown if the <code>String</code> * cannot be parsed as an <code>int</code>. */ public static int parseInt(String s) throws NumberFormatException { return parseInt(s, 10); } /** * Converts the specified <code>String</code> into an <code>int</code> * using the specified radix (base). * * @param s the <code>String</code> to convert * @param radix the radix (base) to use in the conversion * @return the <code>String</code> argument converted to </code>int</code>. * @exception NumberFormatException thrown if the <code>String</code> * cannot be parsed as a <code>int</code>. */ public static int parseInt(String str, int radix) throws NumberFormatException { final int len; if (str == null) throw new NumberFormatException (); if ((len = str.length()) == 0 || radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) throw new NumberFormatException(); boolean isNeg = false; int index = 0; if (str.charAt(index) == '-') if (len > 1) { isNeg = true; index++; } else throw new NumberFormatException(); return parseInt(str, index, len, isNeg, radix); } private static int parseInt(String str, int index, int len, boolean isNeg, int radix) throws NumberFormatException { int val = 0; int digval; int max = MAX_VALUE / radix; // We can't directly write `max = (MAX_VALUE + 1) / radix'. // So instead we fake it. if (isNeg && MAX_VALUE % radix == radix - 1) ++max; for ( ; index < len; index++) { if (val < 0 || val > max) throw new NumberFormatException(); if ((digval = Character.digit(str.charAt(index), radix)) < 0) throw new NumberFormatException(); // Throw an exception for overflow if result is negative. // However, we special-case the most negative value. val = val * radix + digval; if (val < 0 && (! isNeg || val != MIN_VALUE)) throw new NumberFormatException(); } return isNeg ? -(val) : val; } /** * Convert the specified <code>String</code> into an <code>Integer</code>. * The <code>String</code> may represent decimal, hexadecimal, or * octal numbers. * * The <code>String</code> argument is interpreted based on the leading * characters. Depending on what the String begins with (after an optional * minus sign), the base will be interpreted differently: * * <table border=1> * <tr><th>Leading<br>Characters</th><th>Base</th></tr> * <tr><td>#</td><td>16</td></tr> * <tr><td>0x</td><td>16</td></tr> * <tr><td>0X</td><td>16</td></tr> * <tr><td>0</td><td>8</td></tr> * <tr><td>Anything<br>Else</td><td>10</td></tr> * </table> * * If the String starts with a minus sign the result is negated. * * @param str the <code>String</code> to interpret. * @return the value of the String as an <code>Integer</code>. * @exception NumberFormatException thrown if the <code>String</code> * cannot be parsed as an <code>int</code>. */ public static Integer decode(String str) throws NumberFormatException { boolean isNeg = false; int index = 0; int radix = 10; final int len; if ((len = str.length()) == 0) throw new NumberFormatException("empty string"); if (str.charAt(index) == '-') { // The minus sign should be followed by at least one more char if (len > 1) { isNeg = true; index++; } else throw new NumberFormatException(); } if (str.charAt(index) == '#') { radix = 16; index++; } else if (str.charAt(index) == '0') { index++; // Check if str is just "0" or "-0" if (len == index) return new Integer(0); if (str.charAt(index) == 'x' || str.charAt(index) == 'X') { radix = 16; index++; } else radix = 8; } if (index >= len) throw new NumberFormatException("empty value"); return new Integer(parseInt(str, index, len, isNeg, radix)); } /** Return the value of this <code>Integer</code> as a <code>byte</code>. ** @return the value of this <code>Integer</code> as a <code>byte</code>. **/ public byte byteValue() { return (byte) value; } /** Return the value of this <code>Integer</code> as a <code>short</code>. ** @return the value of this <code>Integer</code> as a <code>short</code>. **/ public short shortValue() { return (short) value; } /** Return the value of this <code>Integer</code> as an <code>int</code>. ** @return the value of this <code>Integer</code> as an <code>int</code>. **/ public int intValue() { return value; } /** Return the value of this <code>Integer</code> as a <code>long</code>. ** @return the value of this <code>Integer</code> as a <code>long</code>. **/ public long longValue() { return value; } /** Return the value of this <code>Integer</code> as a <code>float</code>. ** @return the value of this <code>Integer</code> as a <code>float</code>. **/ public float floatValue() { return value; } /** Return the value of this <code>Integer</code> as a <code>double</code>. ** @return the value of this <code>Integer</code> as a <code>double</code>. **/ public double doubleValue() { return value; } /** * Compare two Integers numerically by comparing their * <code>int</code> values. * @return a positive value if this <code>Integer</code> is greater * in value than the argument <code>Integer</code>; a negative value * if this <code>Integer</code> is smaller in value than the argument * <code>Integer</code>; and <code>0</code>, zero, if this * <code>Integer</code> is equal in value to the argument * <code>Integer</code>. * * @since 1.2 */ public int compareTo(Integer i) { if (this.value == i.value) return 0; // Returns just -1 or 1 on inequality; doing math might overflow. if (this.value > i.value) return 1; return -1; } /** * Behaves like <code>compareTo(java.lang.Integer)</code> unless the Object * is not a <code>Integer</code>. Then it throws a * <code>ClassCastException</code>. * @exception ClassCastException if the argument is not a * <code>Integer</code>. * * @since 1.2 */ public int compareTo(Object o) { return compareTo((Integer)o); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?