各位大侠:
    小弟在看<<thinking in java 4th>>的时候,里面有个例子:                Integer int1 = new Integer(47);
Integer int2 = new Integer(47);
System.out.println(int1.equals(int2));结果为true。
这是为什么呢?int1和int2指向2个不同的对象啊,不应该等啊。请大虾们不吝指教啊!想不通

解决方案 »

  1.   

    看源码,这个是比较特殊的    public static Integer valueOf(int i) {
    final int offset = 128;
    if (i >= -128 && i <= 127) { // must cache 
        return IntegerCache.cache[i + offset];
    }
            return new Integer(i);
        }
      

  2.   

    Java自动拆箱  -128~127 Java会自动转换,常说的Java自动装箱
      

  3.   

    int1.equals(int2)比较的是值,当然相等
    看看equals与=的区别
      

  4.   

    equals比较的是2个对象,除非这2个对象是同一个,不然不该等的啊!
      

  5.   

    可是我没有用到valueOf这个方法呀。
      

  6.   

    不好意思,我第一次来java版来发,不知道水深水浅,所以都发了一下 ^_^
      

  7.   


    没,主要是我看错符号。。
    你可以试下int1==int2的结果
      

  8.   

           Integer int1 = 47;
            Integer int2 = 47;
            System.out.println(int1==int2);
      

  9.   


    System.out.println(int1 == int2);结果false;
    所以我说:int1,int2是2个不同的对象,不该等啊
      

  10.   


    equals比较的是2个对象的什么?
      

  11.   

    Integer重写了equals方法,这个方法会比较两个不同Integer对象的值,而不是引用的地址,
    虽然int1和int2是不同的对象,但他们存储的值一样,而equals就是比较的这个值
    如果你想要得到false的结果,就用int1==int2
    ==不是判断对象是否相等,是判断两个对象所引用的内存地址是否是相同的。
      

  12.   

    equals 与 == 没区别
    object 里 equals 里边就是写的 ==
    String里不同是因为重写了这个方法。
    楼主这个问题主要是自动解箱导致的。
      

  13.   


        private final int value;
        public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
        }
        public int intValue() {
    return value;
        }上面是Integer中equals方法的实现,Integer的值存储在private变量value中,int1和int2是不同对象,但int1.value和int2.value都是一样的,都是47,而equals比较的就是value,而不是int1和int2本身
      

  14.   

    equals 比较的是对象引用的内容,==比较的是对象是否相同
      

  15.   


    public class Integer{
    private final int value;
    public Integer(int value) {
    this.value = value;
        }
    public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
        }
    }这个是 API 里的代码
    相信楼主现在知道为什么了吧
      

  16.   

    因为Integer(或者String等)重写了equals方法,所以比较的是值。
    而没有重写equals方法的,就用使用Object的equals方法,比较的是地址。
    所以这里的int1,int2比较的是值,是相等的。
    是这样的吗?
      

  17.   

    除了重写equals方法 
    objec里 equals 方法里边就是简单的用了下 ==/**
         * Indicates whether some other object is "equal to" this one.
         * <p>
         * The <code>equals</code> method implements an equivalence relation
         * on non-null object references:
         * <ul>
         * <li>It is <i>reflexive</i>: for any non-null reference value
         *     <code>x</code>, <code>x.equals(x)</code> should return
         *     <code>true</code>.
         * <li>It is <i>symmetric</i>: for any non-null reference values
         *     <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
         *     should return <code>true</code> if and only if
         *     <code>y.equals(x)</code> returns <code>true</code>.
         * <li>It is <i>transitive</i>: for any non-null reference values
         *     <code>x</code>, <code>y</code>, and <code>z</code>, if
         *     <code>x.equals(y)</code> returns <code>true</code> and
         *     <code>y.equals(z)</code> returns <code>true</code>, then
         *     <code>x.equals(z)</code> should return <code>true</code>.
         * <li>It is <i>consistent</i>: for any non-null reference values
         *     <code>x</code> and <code>y</code>, multiple invocations of
         *     <tt>x.equals(y)</tt> consistently return <code>true</code>
         *     or consistently return <code>false</code>, provided no
         *     information used in <code>equals</code> comparisons on the
         *     objects is modified.
         * <li>For any non-null reference value <code>x</code>,
         *     <code>x.equals(null)</code> should return <code>false</code>.
         * </ul>
         * <p>
         * The <tt>equals</tt> method for class <code>Object</code> implements 
         * the most discriminating possible equivalence relation on objects; 
         * that is, for any non-null reference values <code>x</code> and
         * <code>y</code>, this method returns <code>true</code> if and only
         * if <code>x</code> and <code>y</code> refer to the same object
         * (<code>x == y</code> has the value <code>true</code>).
         * <p>
         * Note that it is generally necessary to override the <tt>hashCode</tt>
         * method whenever this method is overridden, so as to maintain the
         * general contract for the <tt>hashCode</tt> method, which states
         * that equal objects must have equal hash codes. 
         *
         * @param   obj   the reference object with which to compare.
         * @return  <code>true</code> if this object is the same as the obj
         *          argument; <code>false</code> otherwise.
         * @see     #hashCode()
         * @see     java.util.Hashtable
         */
        public boolean equals(Object obj) {
    return (this == obj);
        }
      

  18.   

    主要是因为在-128~127是共享的,所以不管是==还是equals都是true
      

  19.   

    这个问题不需要问了吧。。equals与=的区别
      

  20.   

    这是jdk的新特性.在-127到127都是小的对象,如果都为他分配内存将会不适合,这是一种享元模式,
    将所有的属性相同的对象作为同一个对象,而外部不同的被设计成外部方法,比如在windows文件夹对象,不可能都分配内存空间,而是将不同的位置作为外部行为作为外部状态,相同的作为内部属性.所以在-127到127之间的Integer的比较实际是同一个对象.如果你超过了127或者小于-127他们就不相等了,因为这是比较不常用的对象,jdk会为他分配内存空间.