⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 integer.java

📁 《透视Java》的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        int q, r;        int charPos = 12;        char sign = 0;        if (i < 0) {             sign = '-';            i = -i;        }        // Generate two digits per iteration        while (i >= 65536) {            q = i / 100;        // really: r = i - (q * 100);            r = i - ((q << 6) + (q << 5) + (q << 2));            i = q;            buf [--charPos] = DigitOnes[r];            buf [--charPos] = DigitTens[r];        }        // Fall thru to fast mode for smaller numbers        // assert(i <= 65536, i);        for (;;) {             q = (i * 52429) >>> (16+3);            r = i - ((q << 3) + (q << 1));  // r = i-(q*10) ...            buf [--charPos] = digits [r];            i = q;            if (i == 0) break;        }        if (sign != 0) {            buf [--charPos] = sign;        }        return charPos;    }     static void appendTo(int i, StringBuffer sb) {        switch(i) {            case Integer.MIN_VALUE:                sb.append("-2147483648");                return;            case -3: sb.append("-3"); return;            case -2: sb.append("-2"); return;            case -1: sb.append("-1"); return;            case 0: sb.append("0"); return;            case 1: sb.append("1"); return;            case 2: sb.append("2"); return;            case 3: sb.append("3"); return;            case 4: sb.append("4"); return;            case 5: sb.append("5"); return;            case 6: sb.append("6"); return;            case 7: sb.append("7"); return;            case 8: sb.append("8"); return;            case 9: sb.append("9"); return;            case 10: sb.append("10"); return;        }        char[] buf = (char[])(perThreadBuffer.get());        int charPos = getChars(i, buf);        sb.append(buf, charPos, 12 - charPos);    }        /**     * 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> (<code>'&#92;u002D'</code>) to      * indicate a negative value. The resulting integer value is returned.      * <p>     * An exception of type <code>NumberFormatException</code> is     * thrown if any of the following situations occurs:     * <ul>     * <li>The first argument is <code>null</code> 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> (<code>'&#92;u002D'</code>) provided that the     * string is longer than length 1.     * <li>The value represented by the string is not a value of type     * <code>int</code>.      * </ul><p>     * Examples:     * <blockquote><pre>     * parseInt("0", 10) returns 0     * parseInt("473", 10) returns 473     * 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</code> containing the integer      * 			representation to be parsed     * @param      radix   the radix to be used while parsing <code>s</code>.     * @return     the integer represented by the string argument in the     *             specified radix.     * @exception  NumberFormatException if the <code>String</code>     * 		   does not contain a parsable <code>int</code>.     */    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, max = s.length();	int limit;	int multmin;	int digit;	if (max > 0) {	    if (s.charAt(0) == '-') {		negative = true;		limit = Integer.MIN_VALUE;		i++;	    } else {		limit = -Integer.MAX_VALUE;	    }	    multmin = limit / radix;	    if (i < max) {		digit = Character.digit(s.charAt(i++),radix);		if (digit < 0) {		    throw NumberFormatException.forInputString(s);		} else {		    result = -digit;		}	    }	    while (i < max) {		// 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);	}	if (negative) {	    if (i > 1) {		return result;	    } else {	/* Only got "-" */		throw NumberFormatException.forInputString(s);	    }	} else {	    return -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>      * (<code>'&#92;u002D'</code>) to indicate a negative 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</code> containing the <code>int</code>     *             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.     */    public static int parseInt(String s) throws NumberFormatException {	return parseInt(s,10);    }    /**     * Returns an <code>Integer</code> object holding the value     * extracted from the specified <code>String</code> 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</code> object that     * represents the integer value specified by the string.     * <p>     * In other words, this method returns an <code>Integer</code>     * object equal to the value of:     *     * <blockquote><code>     * new Integer(Integer.parseInt(s, radix))       * </code></blockquote>     *     * @param      s   the string to be parsed.     * @param      radix the radix to be used in interpreting <code>s</code>     * @return     an <code>Integer</code> object holding the value     *             represented by the string argument in the specified     *             radix.     * @exception NumberFormatException if the <code>String</code>     * 		  does not contain a parsable <code>int</code>.     */    public static Integer valueOf(String s, int radix) throws NumberFormatException {	return new Integer(parseInt(s,radix));    }    /**     * Returns an <code>Integer</code> object holding the     * value of the specified <code>String</code>. 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</code> object that represents the integer value     * specified by the string.     * <p>     * In other words, this method returns an <code>Integer</code>     * object equal to the value of:     *     * <blockquote><code>     * new Integer(Integer.parseInt(s))     * </code></blockquote>     *     * @param      s   the string to be parsed.     * @return     an <code>Integer</code> 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 new Integer(parseInt(s, 10));    }    /**     * The value of the <code>Integer</code>.     *     * @serial     */    private int value;    /**     * Constructs a newly allocated <code>Integer</code> object that     * represents the specified <code>int</code> value.     *     * @param   value   the value to be represented by the      *			<code>Integer</code> object.     */    public Integer(int value) {	this.value = value;    }    /**     * Constructs a newly allocated <code>Integer</code> object that     * represents the <code>int</code> value indicated by the     * <code>String</code> parameter. The string is converted to an     * <code>int</code> value in exactly the manner used by the     * <code>parseInt</code> method for radix 10.     *     * @param      s   the <code>String</code> to be converted to an     *                 <code>Integer</code>.     * @exception  NumberFormatException  if the <code>String</code> 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);    }    /**     * Returns the value of this <code>Integer</code> as a     * <code>byte</code>.     */    public byte byteValue() {	return (byte)value;    }    /**     * Returns the value of this <code>Integer</code> as a     * <code>short</code>.     */    public short shortValue() {	return (short)value;    }    /**     * Returns the value of this <code>Integer</code> as an     * <code>int</code>.     */    public int intValue() {	return value;    }    /**     * Returns the value of this <code>Integer</code> as a     * <code>long</code>.     */    public long longValue() {	return (long)value;    }    /**     * Returns the value of this <code>Integer</code> as a     * <code>float</code>.     */    public float floatValue() {	return (float)value;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -