📄 integer.java
字号:
* parseInt("Kona", 10) throws a NumberFormatException * parseInt("Kona", 27) returns 411787 * </pre></blockquote> * * @param s the <code>String</code> containing the integer. * @param radix the radix to be used. * @return the integer represented by the string argument in the * specified radix. * @exception NumberFormatException if the string does not contain a * parsable integer. */ 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 new NumberFormatException(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 new NumberFormatException(s); } if (result < multmin) { throw new NumberFormatException(s); } result *= radix; if (result < limit + digit) { throw new NumberFormatException(s); } result -= digit; } } else { throw new NumberFormatException(s); } if (negative) { if (i > 1) { return result; } else { /* Only got "-" */ throw new NumberFormatException(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> * (<tt>'\u002d'</tt>) 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 string. * @return the integer 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 a new Integer object initialized to the value of the * specified String. 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><pre> * new Integer(Integer.parseInt(s, radix)) * </pre></blockquote> * * @param s the string to be parsed. * @param radix the radix of the integer represented by string * <tt>s</tt> * @return a newly constructed <code>Integer</code> initialized to the * value represented by the string argument in the specified * radix. * @exception NumberFormatException if the String cannot be * parsed as an <code>int</code>. */ public static Integer valueOf(String s, int radix) throws NumberFormatException { return new Integer(parseInt(s,radix)); } /** * Returns a new Integer object initialized to the value of the * specified 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 * <tt>Integer</tt> object that represents the integer value specified * by the string. * <p> * In other words, this method returns an <tt>Integer</tt> object equal * to the value of: * <blockquote><pre> * new Integer(Integer.parseInt(s)) * </pre></blockquote> * * @param s the string to be parsed. * @return a newly constructed <code>Integer</code> initialized to 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 Integer. * * @serial */ private int value; /** * Constructs a newly allocated <code>Integer</code> object that * represents the primitive <code>int</code> argument. * * @param value the value to be represented by the <code>Integer</code>. */ public Integer(int value) { this.value = value; } /** * Returns the value of this Integer as a byte. * * @return the value of this Integer as a byte. * * @since JDK1.1 */ public byte byteValue() { return (byte)value; } /** * Returns the value of this Integer as a short. * * @return the value of this Integer as a short. * * @since JDK1.1 */ public short shortValue() { return (short)value; } /** * Returns the value of this Integer as an int. * * @return the <code>int</code> value represented by this object. */ public int intValue() { return value; } /** * Returns the value of this Integer as a <tt>long</tt>. * * @return the <code>int</code> value represented by this object that is * converted to type <code>long</code> and the result of the * conversion is returned. */ public long longValue() { return (long)value; } /** * Returns a String object representing this 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 String.valueOf(value); } /** * Returns a hashcode for this Integer. * * @return a hash code value for this object, equal to the * primitive <tt>int</tt> value represented by this * <tt>Integer</tt> object. */ public int hashCode() { return value; } /** * Compares this object to the specified object. * The result is <code>true</code> if and only if the argument is not * <code>null</code> and is an <code>Integer</code> object that contains * the same <code>int</code> value as this object. * * @param obj the object to compare with. * @return <code>true</code> if the objects are the same; * <code>false</code> otherwise. */ public boolean equals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -