今天看第2遍,又遇到个问题 
public class Equals
{
public static void main(String [] args)
{
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1.equals(n2));

A a = new A();
a.i=47;
A b = new A();
b.i=47;
System.out.println(a.equals(b));
}
}
class A
{
int i;
}说是equals()比较的是内容,那n1是等于n2 
可是a和b的内容也是一样的啊  为什么就是flase呢
如果说a和b的内容不一样,那哪不一样呢
或者还是equlas()比较的是别的?我看了Object的代码,他里边的eaulas()是 return this==obj; 
那n1.equals(n2)----->return n1=n2 那还是应该false啊 n1和n2肯定一样的啊奇怪了?

解决方案 »

  1.   

    个人认为,你New了两个A类的实例,就是说 a 和 b指向的地址是不一样的,所以用equals比较这两个对象出来的结果是false,
      如果改成: A a= new A();
                a.i=47;
                A b = new A();
                b = a;
    System.out.println(a.equals(b));
      

  2.   

    可是Integer n1 = new Integer(47);
    Integer n2 = new Integer(47); n1和n2的地址也是不一样的 为什么是true呢
      

  3.   

    说是equals()比较的是内容这不错啊,但这是对Object类的equals()做覆盖才可以实现的
    也就是说在Object类中的equals()方法默认的还是比较引用:
    Integer n1 = new Integer(47);
    Integer n2 = new Integer(47);
    这两句定义包裹类型对象里面的内容是相等的,而且这个包裹类也对equals()方法做了覆盖这时比较的是内容
    System.out.println(n1.equals(n2));返回的当然是true了。而下面这些语句:
    A a = new A();
    a.i=47;
    A b = new A();
    b.i=47;
    这两个对象里内容虽然相等,但是调用的是Object类的equals()的方法比较的还是引用,如果想实现我们希望的结果(也就是实现内容的比较),你得在类A里覆盖equals()方法
    因此
    System.out.println(a.equals(b));输出的是false,默认比较的还是引用你再看看这下面这个:
    Object ob1=new Object();
    Object ob2=new Object();
    //这输出的是false因为在object里equals()方法比较的还是引用
    System.out.println(ob1.equals(ob2));String st1=new String("hello");
    String st2=new String("hello");
    //这里输出的true因为String类覆盖了object里的equals()方法,比较的是内容了
    System.out.println(st1.equals(st2));
      

  4.   

    你的A类没有重写equals,而Integer重写了。
      

  5.   

    To: lingwen20(▄︻┻┳═一)String st1=new String("hello");
    String st2=new String("hello");
    //这里输出的true因为String类覆盖了object里的equals()方法,比较的是内容了
    System.out.println(st1.equals(st2));string的equals没有被覆盖,一样是因为编译时优化的原因,内存中只有一个hello串。
    String st2 = "";
    st2 += "h";
    st2 += "e";
    st2 += "l";
    st2 += "l";
    st2 += "o";
    你再试试,嘿嘿,不要被骗啊!
      

  6.   

    lingwen20(▄︻┻┳═一)说的很对~~~须要重写equals,不重写的话比较的是reference.
      

  7.   

    gzpoplar(poplar)你说的也有道理
    你试试这个例子,看看能不能明白里面的缘由?
    public class Equals
    {
    public static void main(String [] args)
    {
        String st1=new String("hello");
        String st2=new String("hello");
        String st3="hello";
        String st4="hello";
    //注意各种比较的结果
        System.out.println("st1.equals(st2):\t"+(st1.equals(st2)));//true
        System.out.println("st1==st2:\t"+(st1==st2));//false
        System.out.println("st3==st4:\t"+(st3==st4));//true
        System.out.println("st3.equals(st4):\t"+(st3.equals(st4)));//true
        System.out.println("st1==st3:\t"+(st1==st3));//false
        System.out.println("st1.equals(st3):\t"+(st1.equals(st3)));//true
        
        String st5="";
        st5+="h";
        st5+="e";
        st5+="l";
        st5+="l";
        st5+="l";
        st5+="o";
        System.out.println("st5:\t"+st5);
        
        String st6=new String();
        st6+="h";
        st6+="e";
        st6+="l";
        st6+="l";
        st6+="l";
        st6+="o";
        System.out.println("st6:\t"+st6);
        /*Integer n1 = new Integer(47);
    Integer n2 = new Integer(47);
    System.out.println(n1.equals(n2));A a = new A();
    a.i=47;
    A b = new A();
    b.i=47;
    System.out.println(a.equals(b));*/
    }
    }
    /*class A
    {
    int i; 
    }*/
    其实对String类来说
    用new与不用new来创建字符串是不同的,
      String st1=new String("hello");//这是在堆里创建的对象,与下句创建的String对象是互不相同的
        String st2=new String("hello");
        String st3="hello";//这是在池里创建的字符串,字符串常量会放入字符串常量池中
        String st4="hello";
      

  8.   

    equals这个方法是来自于java.lang.object,它不用作对象的比较,而不是你所认为的a.i=47等价于b.i=47,这两个i的值是相等的,但equals比较的是对象a与对象b,a与b之间不存在引用的关系,所以程序所得到的结果是false
      

  9.   

    更正一下,上面多打了个"不"字
    equals这个方法是来自于java.lang.object,它用作对象的比较,而不是你所认为的a.i=47等价于b.i=47,这两个i的值是相等的,但equals比较的是对象a与对象b,a与b之间不存在引用的关系,所以程序所得到的结果是false
      

  10.   

    equals方法用的地方多了。
    其主要目的就是为了两个对象之前的比较。
    默认的,就是在Object中定义 的比较方法为两个对象是否是同一个对象,和值没有关系。用户呢可以去重写这个方法,而想怎么比较就看用户自己怎么写了。
    而有一些类java中也是重写了的。比如:Integer之类的数据类型的这些类。