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

📄 integer.java

📁 《透视Java》的源码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    /**     * Returns the value of this <code>Integer</code> as a     * <code>double</code>.     */    public double doubleValue() {	return (double)value;    }    /**     * Returns a <code>String</code> object representing this     * <code>Integer</code>'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&nbsp;10.     */    public String toString() {	return String.valueOf(value);    }    /**     * Returns a hash code for this <code>Integer</code>.     *     * @return  a hash code value for this object, equal to the      *          primitive <code>int</code> value represented by this      *          <code>Integer</code> 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;    }    /**     * Determines the integer value of the system property with the     * specified name.     * <p>     * The first argument is treated as the name of a system property.      * System properties are accessible through the      * {@link java.lang.System#getProperty(java.lang.String)} method. The      * string value of this property is then interpreted as an integer      * value and an <code>Integer</code> object representing this value is      * returned. Details of possible numeric formats can be found with      * the definition of <code>getProperty</code>.      * <p>     * If there is no property with the specified name, if the specified name     * is empty or <code>null</code>, or if the property does not have      * the correct numeric format, then <code>null</code> is returned.     * <p>     * In other words, this method returns an <code>Integer</code>     * object equal to the value of:     *     * <blockquote><code>     * getInteger(nm, null)     * </code></blockquote>     *     * @param   nm   property name.     * @return  the <code>Integer</code> value of the property.     * @see     java.lang.System#getProperty(java.lang.String)     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)     */    public static Integer getInteger(String nm) {	return getInteger(nm, null);    }    /**     * Determines the integer value of the system property with the     * specified name.     * <p>     * The first argument is treated as the name of a system property.     * System properties are accessible through the {@link     * java.lang.System#getProperty(java.lang.String)} method. The      * string value of this property is then interpreted as an integer      * value and an <code>Integer</code> object representing this value is      * returned. Details of possible numeric formats can be found with      * the definition of <code>getProperty</code>.      * <p>     * The second argument is the default value. An <code>Integer</code> object     * that represents the value of the second argument is returned if there     * is no property of the specified name, if the property does not have     * the correct numeric format, or if the specified name is empty or     *  <code>null</code>.     * <p>     * In other words, this method returns an <code>Integer</code> object      * equal to the value of:     * <blockquote><code>     * getInteger(nm, new Integer(val))     * </code></blockquote>     * but in practice it may be implemented in a manner such as:      * <blockquote><pre>     * Integer result = getInteger(nm, null);     * return (result == null) ? new Integer(val) : result;     * </pre></blockquote>     * to avoid the unnecessary allocation of an <code>Integer</code>      * object when the default value is not needed.      *     * @param   nm   property name.     * @param   val   default value.     * @return  the <code>Integer</code> value of the property.     * @see     java.lang.System#getProperty(java.lang.String)     * @see     java.lang.System#getProperty(java.lang.String, java.lang.String)     */    public static Integer getInteger(String nm, int val) {        Integer result = getInteger(nm, null);        return (result == null) ? new Integer(val) : result;    }    /**     * Returns the integer value of the system property with the     * specified name.  The first argument is treated as the name of a     * system property.  System properties are accessible through the     * {@link java.lang.System#getProperty(java.lang.String)} method.     * The string value of this property is then interpreted as an     * integer value, as per the <code>Integer.decode</code> method,     * and an <code>Integer</code> object representing this value is     * returned.     * <p>     * <ul><li>If the property value begins with the two ASCII characters      *         <code>0x</code> or the ASCII character <code>#</code>, not      *      followed by a minus sign, then the rest of it is parsed as a      *      hexadecimal integer exactly as by the method      *      {@link #valueOf(java.lang.String, int)} with radix 16.      * <li>If the property value begins with the ASCII character      *     <code>0</code> followed by another character, it is parsed as an      *     octal integer exactly as by the method      *     {@link #valueOf(java.lang.String, int)} with radix 8.      * <li>Otherwise, the property value is parsed as a decimal integer      * exactly as by the method {@link #valueOf(java.lang.String, int)}      * with radix 10.      * </ul><p>     * The second argument is the default value. The default value is     * returned if there is no property of the specified name, if the     * property does not have the correct numeric format, or if the     * specified name is empty or <code>null</code>.     *     * @param   nm   property name.     * @param   val   default value.     * @return  the <code>Integer</code> value of the property.     * @see     java.lang.System#getProperty(java.lang.String)     * @see java.lang.System#getProperty(java.lang.String, java.lang.String)     * @see java.lang.Integer#decode     */    public static Integer getInteger(String nm, Integer val) {	String v = null;        try {            v = System.getProperty(nm);        } catch (IllegalArgumentException e) {        } catch (NullPointerException e) {        }	if (v != null) {	    try {		return Integer.decode(v);	    } catch (NumberFormatException e) {	    }	}	return val;    }    /**     * Decodes a <code>String</code> into an <code>Integer</code>.     * Accepts decimal, hexadecimal, and octal numbers 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</code> <i>HexDigits</i>     * <dd><i>Sign<sub>opt</sub></i> <code>0X</code> <i>HexDigits</i>     * <dd><i>Sign<sub>opt</sub></i> <code>#</code> <i>HexDigits</i>     * <dd><i>Sign<sub>opt</sub></i> <code>0</code> <i>OctalDigits</i>     * <p>     * <dt><i>Sign:</i>     * <dd><code>-</code>     * </dl>     * </blockquote>     *     * <i>DecimalNumeral</i>, <i>HexDigits</i>, and <i>OctalDigits</i>     * are defined in <a href="http://java.sun.com/docs/books/jls/second_edition/html/lexical.doc.html#48282">&sect;3.10.1</a>      * of the <a href="http://java.sun.com/docs/books/jls/html/">Java      * Language Specification</a>.     * <p>     * The sequence of characters following an (optional) negative     * sign and/or radix specifier (&quot;<code>0x</code>&quot;,     * &quot;<code>0X</code>&quot;, &quot;<code>#</code>&quot;, or     * leading zero) is parsed as by the <code>Integer.parseInt</code>     * 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</code> is the     * minus sign.  No whitespace characters are permitted in the     * <code>String</code>.     *     * @param     nm the <code>String</code> to decode.     * @return    a <code>Integer</code> object holding the <code>int</code>     *		   value represented by <code>nm</code>     * @exception NumberFormatException  if the <code>String</code> does not     *            contain a parsable integer.     * @see java.lang.Integer#parseInt(java.lang.String, int)     * @since 1.2     */    public static Integer decode(String nm) throws NumberFormatException {        int radix = 10;        int index = 0;        boolean negative = false;        Integer result;        // Handle minus sign, if present        if (nm.startsWith("-")) {            negative = true;            index++;        }        // Handle radix specifier, if present	if (nm.startsWith("0x", index) || nm.startsWith("0X", index)) {	    index += 2;            radix = 16;	}	else if (nm.startsWith("#", index)) {	    index ++;            radix = 16;	}	else if (nm.startsWith("0", index) && nm.length() > 1 + index) {	    index ++;            radix = 8;	}        if (nm.startsWith("-", index))            throw new NumberFormatException("Negative sign in wrong position");        try {            result = Integer.valueOf(nm.substring(index), radix);            result = negative ? new Integer(-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 ? new String("-" + nm.substring(index))                                       : nm.substring(index);            result = Integer.valueOf(constant, radix);        }        return result;    }    /**     * Compares two <code>Integer</code> objects numerically.     *     * @param   anotherInteger   the <code>Integer</code> to be compared.     * @return	the value <code>0</code> if this <code>Integer</code> is     * 		equal to the argument <code>Integer</code>; a value less than     * 		<code>0</code> if this <code>Integer</code> is numerically less     * 		than the argument <code>Integer</code>; and a value greater      * 		than <code>0</code> if this <code>Integer</code> is numerically     * 		 greater than the argument <code>Integer</code> (signed     * 		 comparison).     * @since   1.2     */    public int compareTo(Integer anotherInteger) {	int thisVal = this.value;	int anotherVal = anotherInteger.value;	return (thisVal<anotherVal ? -1 : (thisVal==anotherVal ? 0 : 1));    }    /**     * Compares this <code>Integer</code> object to another object.     * If the object is an <code>Integer</code>, this function behaves     * like <code>compareTo(Integer)</code>.  Otherwise, it throws a     * <code>ClassCastException</code> (as <code>Integer</code>     * objects are only comparable to other <code>Integer</code>     * objects).     *     * @param   o the <code>Object</code> to be compared.     * @return  the value <code>0</code> if the argument is a      *		<code>Integer</code> numerically equal to this      *		<code>Integer</code>; a value less than <code>0</code>      *		if the argument is a <code>Integer</code> numerically      *		greater than this <code>Integer</code>; and a value      *		greater than <code>0</code> if the argument is a      *		<code>Integer</code> numerically less than this      *		<code>Integer</code>.     * @exception <code>ClassCastException</code> if the argument is not an     *		  <code>Integer</code>.     * @see     java.lang.Comparable     * @since   1.2     */    public int compareTo(Object o) {	return compareTo((Integer)o);    }    /** use serialVersionUID from JDK 1.0.2 for interoperability */    private static final long serialVersionUID = 1360826667806852920L;    /**     * Sets the value of this Integer.     * *** Added by Alex Kalinovsky ***     *     * @param   value the new value for this integer object     */    public void setValue(int value) {        this.value = value;    }}

⌨️ 快捷键说明

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