// 值比较 System.out.println(("a equals b is " + (a.equals(b)))); // a equals b is true
// 引用比较,结果为 false System.out.println("a == b is " + (a == b)); // a == b is false // 引用比较,结果意外的为 true。这是由于 Integer 自动装箱时 [-128, 127] 使用了缓存,详见其 valueOf 方法的源码实现。 System.out.println(("c == d is " + (c == d))); // c == d is true // 引用比较,结果为 false。因为 new Integer(int) 构造方法没有使用缓存。 System.out.println(("e == f is " + (e == f))); // e == f is false
/** * The value of the {@code Byte}. * * @serial */ privatefinalbyte value;
/** * Returns a {@code Byte} instance representing the specified * {@code byte} value. * If a new {@code Byte} instance is not required, this method * should generally be used in preference to the constructor * {@link #Byte(byte)}, as this method is likely to yield * significantly better space and time performance since * all byte values are cached. * * @param b a byte value. * @return a {@code Byte} instance representing {@code b}. * @since 1.5 */ publicstatic Byte valueOf(byte b) { finalintoffset=128; return ByteCache.cache[(int)b + offset]; }
/** * Returns the value of this {@code Byte} as a * {@code byte}. */ publicbytebyteValue() { return value; }
/** * Compares this object to the specified object. The result is * {@code true} if and only if the argument is not * {@code null} and is a {@code Byte} object that * contains the same {@code byte} value as this object. * * @param obj the object to compare with * @return {@code true} if the objects are the same; * {@code false} otherwise. */ publicbooleanequals(Object obj) { if (obj instanceof Byte) { return value == ((Byte)obj).byteValue(); } returnfalse; } }
/** * The value of the {@code Short}. * * @serial */ privatefinalshort value;
/** * Returns a {@code Short} instance representing the specified * {@code short} value. * If a new {@code Short} instance is not required, this method * should generally be used in preference to the constructor * {@link #Short(short)}, 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 s a short value. * @return a {@code Short} instance representing {@code s}. * @since 1.5 */ publicstatic Short valueOf(short s) { finalintoffset=128; intsAsInt= s; if (sAsInt >= -128 && sAsInt <= 127) { // must cache return ShortCache.cache[sAsInt + offset]; } returnnewShort(s); }
/** * Returns the value of this {@code Short} as a * {@code short}. */ publicshortshortValue() { return value; }
/** * Compares this object to the specified object. The result is * {@code true} if and only if the argument is not * {@code null} and is a {@code Short} object that * contains the same {@code short} value as this object. * * @param obj the object to compare with * @return {@code true} if the objects are the same; * {@code false} otherwise. */ publicbooleanequals(Object obj) { if (obj instanceof Short) { return value == ((Short)obj).shortValue(); } returnfalse; }
/** * The value of the {@code Integer}. * * @serial */ privatefinalint value;
/** * 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 */ publicstatic Integer valueOf(int i) { if (i >= IntegerCache.low && i <= IntegerCache.high) return IntegerCache.cache[i + (-IntegerCache.low)]; returnnewInteger(i); }
/** * Returns the value of this {@code Integer} as an * {@code int}. */ publicintintValue() { return value; }
/** * Compares this object to the specified object. The result is * {@code true} if and only if the argument is not * {@code null} and is an {@code Integer} object that * contains the same {@code int} value as this object. * * @param obj the object to compare with. * @return {@code true} if the objects are the same; * {@code false} otherwise. */ publicbooleanequals(Object obj) { if (obj instanceof Integer) { return value == ((Integer)obj).intValue(); } returnfalse; }
/** * The value of the {@code Long}. * * @serial */ privatefinallong value;
/** * Returns a {@code Long} instance representing the specified * {@code long} value. * If a new {@code Long} instance is not required, this method * should generally be used in preference to the constructor * {@link #Long(long)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * Note that unlike the {@linkplain valueOf(int) * corresponding method} in the {@code Integer} class, this method * is <em>not</em> required to cache values within a particular * range. * * @param l a long value. * @return a {@code Long} instance representing {@code l}. * @since 1.5 */ publicstatic Long valueOf(long l) { finalintoffset=128; if (l >= -128 && l <= 127) { // will cache return LongCache.cache[(int)l + offset]; } returnnewLong(l); }
/** * Returns the value of this {@code Long} as a * {@code long} value. */ publiclonglongValue() { return value; }
/** * Compares this object to the specified object. The result is * {@code true} if and only if the argument is not * {@code null} and is a {@code Long} object that * contains the same {@code long} value as this object. * * @param obj the object to compare with. * @return {@code true} if the objects are the same; * {@code false} otherwise. */ publicbooleanequals(Object obj) { if (obj instanceof Long) { return value == ((Long)obj).longValue(); } returnfalse; }
/** * The value of the Float. * * @serial */ privatefinalfloat value;
/** * Returns a {@code Float} instance representing the specified * {@code float} value. * If a new {@code Float} instance is not required, this method * should generally be used in preference to the constructor * {@link #Float(float)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param f a float value. * @return a {@code Float} instance representing {@code f}. * @since 1.5 */ publicstatic Float valueOf(float f) { returnnewFloat(f); }
/** * Compares this object against the specified object. The result * is {@code true} if and only if the argument is not * {@code null} and is a {@code Float} object that * represents a {@code float} with the same value as the * {@code float} represented by this object. For this * purpose, two {@code float} values are considered to be the * same if and only if the method {@link #floatToIntBits(float)} * returns the identical {@code int} value when applied to * each. * * <p>Note that in most cases, for two instances of class * {@code Float}, {@code f1} and {@code f2}, the value * of {@code f1.equals(f2)} is {@code true} if and only if * * <blockquote><pre> * f1.floatValue() == f2.floatValue() * </pre></blockquote> * * <p>also has the value {@code true}. However, there are two exceptions: * <ul> * <li>If {@code f1} and {@code f2} both represent * {@code Float.NaN}, then the {@code equals} method returns * {@code true}, even though {@code Float.NaN==Float.NaN} * has the value {@code false}. * <li>If {@code f1} represents {@code +0.0f} while * {@code f2} represents {@code -0.0f}, or vice * versa, the {@code equal} test has the value * {@code false}, even though {@code 0.0f==-0.0f} * has the value {@code true}. * </ul> * * This definition allows hash tables to operate properly. * * @param obj the object to be compared * @return {@code true} if the objects are the same; * {@code false} otherwise. * @see java.lang.floatToIntBits(float) */ publicbooleanequals(Object obj) { return (obj instanceof Float) && (floatToIntBits(((Float)obj).value) == floatToIntBits(value)); }
/** * The value of the Double. * * @serial */ privatefinaldouble value;
/** * Returns a {@code Double} instance representing the specified * {@code double} value. * If a new {@code Double} instance is not required, this method * should generally be used in preference to the constructor * {@link #Double(double)}, as this method is likely to yield * significantly better space and time performance by caching * frequently requested values. * * @param d a double value. * @return a {@code Double} instance representing {@code d}. * @since 1.5 */ publicstatic Double valueOf(double d) { returnnewDouble(d); }
/** * Compares this object against the specified object. The result * is {@code true} if and only if the argument is not * {@code null} and is a {@code Double} object that * represents a {@code double} that has the same value as the * {@code double} represented by this object. For this * purpose, two {@code double} values are considered to be * the same if and only if the method {@link * #doubleToLongBits(double)} returns the identical * {@code long} value when applied to each. * * <p>Note that in most cases, for two instances of class * {@code Double}, {@code d1} and {@code d2}, the * value of {@code d1.equals(d2)} is {@code true} if and * only if * * <blockquote> * {@code d1.doubleValue() == d2.doubleValue()} * </blockquote> * * <p>also has the value {@code true}. However, there are two * exceptions: * <ul> * <li>If {@code d1} and {@code d2} both represent * {@code Double.NaN}, then the {@code equals} method * returns {@code true}, even though * {@code Double.NaN==Double.NaN} has the value * {@code false}. * <li>If {@code d1} represents {@code +0.0} while * {@code d2} represents {@code -0.0}, or vice versa, * the {@code equal} test has the value {@code false}, * even though {@code +0.0==-0.0} has the value {@code true}. * </ul> * This definition allows hash tables to operate properly. * @param obj the object to compare with. * @return {@code true} if the objects are the same; * {@code false} otherwise. * @see java.lang.Double#doubleToLongBits(double) */ publicbooleanequals(Object obj) { return (obj instanceof Double) && (doubleToLongBits(((Double)obj).value) == doubleToLongBits(value)); }
/** * The value of the Boolean. * * @serial */ privatefinalboolean value;
/** * Returns a {@code Boolean} instance representing the specified * {@code boolean} value. If the specified {@code boolean} value * is {@code true}, this method returns {@code Boolean.TRUE}; * if it is {@code false}, this method returns {@code Boolean.FALSE}. * If a new {@code Boolean} instance is not required, this method * should generally be used in preference to the constructor * {@link #Boolean(boolean)}, as this method is likely to yield * significantly better space and time performance. * * @param b a boolean value. * @return a {@code Boolean} instance representing {@code b}. * @since 1.4 */ publicstatic Boolean valueOf(boolean b) { return (b ? TRUE : FALSE); }
/** * Returns the value of this {@code Boolean} object as a boolean * primitive. * * @return the primitive {@code boolean} value of this object. */ publicbooleanbooleanValue() { return value; }
/** * Returns {@code true} if and only if the argument is not * {@code null} and is a {@code Boolean} object that * represents the same {@code boolean} value as this object. * * @param obj the object to compare with. * @return {@code true} if the Boolean objects represent the * same value; {@code false} otherwise. */ publicbooleanequals(Object obj) { if (obj instanceof Boolean) { return value == ((Boolean)obj).booleanValue(); } returnfalse; }
/** * The value of the {@code Character}. * * @serial */ privatefinalchar value;
/** * Returns a <tt>Character</tt> instance representing the specified * <tt>char</tt> value. * If a new <tt>Character</tt> instance is not required, this method * should generally be used in preference to the constructor * {@link #Character(char)}, 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 {@code * '\u005Cu0000'} to {@code '\u005Cu007F'}, inclusive, and may * cache other values outside of this range. * * @param c a char value. * @return a <tt>Character</tt> instance representing <tt>c</tt>. * @since 1.5 */ publicstatic Character valueOf(char c) { if (c <= 127) { // must cache return CharacterCache.cache[(int)c]; } returnnewCharacter(c); }
/** * Returns the value of this {@code Character} object. * @return the primitive {@code char} value represented by * this object. */ publiccharcharValue() { return value; }
/** * Compares this object against the specified object. * The result is {@code true} if and only if the argument is not * {@code null} and is a {@code Character} object that * represents the same {@code char} value as this object. * * @param obj the object to compare with. * @return {@code true} if the objects are the same; * {@code false} otherwise. */ publicbooleanequals(Object obj) { if (obj instanceof Character) { return value == ((Character)obj).charValue(); } returnfalse; }