import java.util.*;class ghyghost {
int i;}public class abc { public static void main(String[] args) {
Integer abc = new Integer(10);
Integer xyz = new Integer(10);
System.out.println(abc.equals(xyz)); ghyghost aa = new ghyghost();
ghyghost bb = new ghyghost();
aa.i = bb.i = 100;
System.out.println(aa.equals(bb));
}
} // /:~最近在看T I J(第二版)这本书,有一个例子,,
结果为true false问题是:书上讲equals缺省是引用比较,99页。。
我的想法总和这个结果不一样,请各位高手讲一讲,,这里面是什么原理,感谢:)请教了。

解决方案 »

  1.   

    我是这么理解的:Integer abc = new Integer(10);
    Integer xyz = new Integer(10);
    System.out.println(abc.equals(xyz));
    ----------------------------
    这段代码由于都创建了10这个常数,所以在JAVA的常量池中只有一个位置的10,这时abc和xyz在编译器智能编译的情况下都指向了10这个内存位置,所以讲:书上讲equals缺省是引用比较。。这句话是对的。。
    再看这段代码:
    ghyghost aa = new ghyghost();
    ghyghost bb = new ghyghost();
    aa.i = bb.i = 100;
    System.out.println(aa.equals(bb));
    ------------------------------------------------------
    还是这句话:书上讲equals缺省是引用比较~~。如果按照这句话的意思,肯定是false了,因为是二个对象,不知道这么解释对不对??
    难道用户自定义的类的对象没有常量池这个概念??如果这样就解释通了,,还请各位高手帮忙解释一下。。
      

  2.   

    import java.util.*;class ghyghost {
    int i;}public class abc { public static void main(String[] args) {
    Integer abc = new Integer(10);
    Integer xyz = new Integer(10);
    System.out.println(abc.equals(xyz)); ghyghost aa = new ghyghost();
    ghyghost bb = new ghyghost();
    aa.i = bb.i = 100;
    System.out.println(aa.equals(bb)); aa = bb;
    System.out.println(aa.equals(bb));
    }
    } // /:~
    true
    false
    true
    书上讲equals缺省是引用比较~~。
    这句话完全正确呀,呵呵
      

  3.   

    首先我不是高手:)应该是Integer覆盖了Object的equals呀
    public boolean equals(Object obj)
    Compares this object to the specified object. The result is true if and only if the argument is not null and is an Integer object that contains the same int value as this object.
      

  4.   

    感谢楼上,,,那意思是在JDK的内部Integer类已经写好了override的equals方法??能找到JDK的源代码来证明吗??楼下的朋友如何理解,,
      

  5.   

    import java.util.*;class ghyghost {
    int i;}public class abc { public static void main(String[] args) {
    Integer abc = new Integer(10);
    Integer xyz = new Integer(10);
    System.out.println(abc.equals(xyz));
    System.out.println(abc==xyz); ghyghost aa = new ghyghost();
    ghyghost bb = new ghyghost();
    aa.i = bb.i = 100;
    System.out.println(aa.equals(bb)); aa = bb;
    System.out.println(aa.equals(bb));
    }
    } // /:~true
    false
    false
    true
    呵呵,看来我理解有误 呀,,,,答案第二行false是结果,,说明abc和xyz是二个对象,并没有指向同一个10的常量池地址。难道类Integer真的在内部override了equals??期待其它朋友帮忙,请教了:)
      

  6.   

    equals是一个类的基本方法
    JAVA的类基本上都重写了这个方法
      

  7.   

    JAVA的类基本上都重写了这个方法??基本上都重写了???那什么类没有重写??感谢
      

  8.   

    public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
        } 
    类Integer重写了equals方法,在JDK代码中找到的。。呵呵,,不知道是不是最后的答案
      

  9.   

    源代码如下(Integer.class文件中706-711行);
        public boolean equals(Object obj) {
    if (obj instanceof Integer) {
        return value == ((Integer)obj).intValue();
    }
    return false;
        }