第一个不用说了把?第二个你看看,那是String么?明明是对象的地址嘛,怎么可能一样啊?f1.num 和 f2.num 这才是String,你equals 下看看是不是一样。看java的API,java.lang.Object.equals(Object obj)这样描述:The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values x and y, this method returns true if and only if x and y refer to the same object (x==y has the value true). 只有 f1 和 f2 指向同一个对象的时候才比较得 true,也就是地址相等。而不是什么对象的结构内容。至于String的比较,f1和f2地址是不同的,不过这里比较的却是内容,为什么是这样,我也不太清楚,可能要问SUN是怎么发明的了。不过对于普通的一个类的实例,确实是比较引用的地址的,也就是是不是指向同一个咚咚。也许,我猜测,equals是不是比较引用指向的内容,对于String,引用的地址不一样,可是指向的内容的地址却一样?(因为常量池的缘故?)对于一般的object,引用指向的内容的地址就不一样了,因为那可不是什么String,没什么常量池了。关于常量池。可以看String.intern()的API说明:String java.lang.String.intern()Returns a canonical representation for the string object. 
A pool of strings, initially empty, is maintained privately by the class String. 
When the intern method is invoked, if the pool already contains a string equal to this String object 
 as determined by the .equals(Object) method, then the string from the pool is returned. 
 Otherwise, this String object is added to the pool and a reference to this String object is returned. 
It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is 
 true. 

解决方案 »

  1.   

    class Equal{
    public static void main(String[] args){
    String s1=new String("abc");
    String s2=new String("abc");
    if(s1.equals(s2))
          System.out.println("s1=s2");//原因是s1的对象和s2的对象内容相同
    else                              //(abc).equals(abc)==true
          System.out.println("s1!=s2");
    }
    }
    _________________________________________________________________________________________
    class Func{
    private String num;
    public Func(String n){
    num=n;
    }
    }
    class TFunc{
    public static void main(String[] args){
    Func f1=new Func("abc");
    Func f2=new Func("abc");
    if(f1.equals(f2))
                   System.out.println("f1=f2");
    else
          System.out.println("f1!=f2");
    }
    }
    当然会答应出f1!=f2,理由是“abc”只是对Func 的对象f1构造函数的参数,而不是对象的内容,不能看他相等就说内容一致,况且是String,不是Func型,如果是(f1.num).equals(f2.num)==true;
    ——————————————————————————————————————————--
    我扩展如下
    class Func{
    public Func(){
    }
     
    public Func(Func n){
     }
     
     
    }
    public class Test{
    static Func f3,f4;
    public static void main(String[] args){
     
    Func f1=new Func(f3);System.out.println(f1);

    Func f2=new Func(f4);System.out.println(f2);
    if(f1.equals(f2))
                   System.out.println("f1=f2");
    else
          System.out.println("f1!=f2");
    }
    }
    看看运行结果,就可以知道f1和f2是两个不同的对象返回来的名字不一样,应该也算内容吧
    --------------------------------------------
    建议楼主号召多些人来讨论,呵呵
      

  2.   

    对于2楼的jackyzgm(昵称)只有 f1 和 f2 指向同一个对象的时候才比较得 true,也就是地址相等。而不是什么对象的结构内容。看java的API,java.lang.Object.equals(Object obj)你这个是对象的equals,这个不同于String类的equals,String类的equals是重写了Object的equals的,我给出了下面的例子class Func{
    Func n;
     
    public Func(){
     
    }
     
    public Func(Func n){
    this.n= n;
     }
     
     
    }
    public class Test{
     
     
    public static void main(String[] args){   
    Func f3=null,f4=null;
    f4=f3;
     
    Func f1=new Func(f3);System.out.println(f1);

    Func f2=new Func(f4);System.out.println(f2);
    if(f1.equals(f2))
                   System.out.println("f1=f2");
    else
          System.out.println("f1!=f2");
    }
    }
    结果照样是f1!=f2
      

  3.   

    String 类 equals()
    public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
    if (anObject instanceof String) {
        String anotherString = (String)anObject;
        int n = count;
        if (n == anotherString.count) {
    char v1[] = value;
    char v2[] = anotherString.value;
    int i = offset;
    int j = anotherString.offset;
    while (n-- != 0) {
        if (v1[i++] != v2[j++])
    return false;
    }
    return true;
        }
    }
    return false;
        }
      

  4.   

    jackyzgm(昵称) :
    也许,我猜测,equals是不是比较引用指向的内容,对于String,引用的地址不一样,可是指向的内容的地址却一样?(因为常量池的缘故?)对于一般的object,引用指向的内容的地址就不一样了,因为那可不是什么String,没什么常量池了。
    常量池??????
    这哪跟哪?
    同意classjava(原始野人)
    String类的equals,String类的equals是重写了Object的equals
    你那书太烂,扔了!
      

  5.   

    equals在比较string 的时候可以直接用,但是到类的比较的时候要对类进行复写
    equals方法,否则是不正确的。
      

  6.   

    1.equals是Object的方法,如果不重写,equals和==是一样的功能
    2.一般的类都重写了equals方法,自定义类一般也要重写这个方法
      

  7.   

    另外如果在同一个JVM,对于单例模式一般不需要重写equals.
    但是如果要用于网络的话,仍然要重写equals.
      

  8.   

    == 比较两个要比较的东西是否是同一个
    equals 要看对象的具体怎么实现这个方法的就这么简单啊
      

  9.   

    同意楼上,对于自定义类要重写equals方法,否则调用父类默认的equals方法。
    对于Object类的equals方法,它提供了一个最最严密的实现,就是只有是同一对象时,equals方法才返回
    true,也就是人们常说的引用比较而不是值比较.在具体子类(相对于Object来说)中,如果我们要进行对象的值比较,就必须实现自己的equals方法.
      

  10.   

    谢谢各位兄弟的回复。
    因为是菜鸟,所以在看初级的书。当然没有thinking in java写的那么详细。
    因为是菜鸟,所以问一些初级的问题也不为过吧。
    谢谢!!
      

  11.   

    “==”操作符用于比较两个变量的值是否相等,
    “equals()”方法用于比较两个对象内容是否一致、(书上这么写的。呵呵!)
    ---------------------------------------
    这样的翻译真是……
    如果指明了是String类还好点
      

  12.   

    == 是判断两个变量是不是指向同一个内存地址
    equals 是一个可以被子类所重载的方法,它默认的实现等同于 == ,但是如Integer,String都重写了该方法——如果内容相等返回true
      

  13.   

    class Func{
        private String num;
        public Func(String n){
            num=n;
        }
    }
    class TFunc{
        public static void main(String[] args){
            Func f1=new Func("abc");
            Func f2=new Func("abc");
            if(f1.equals(f2))
                System.out.println("f1=f2");
            else
                System.out.println("f1!=f2");
        }
    }
    -------------------------------------------------------------
    其中的f1、f2只是一个Func的实例,而Func类本身只是一个Object。
    在Java里面的Object.equals(Object o)方法默认是比较reference的。
    所以f1!=f2.
    如果你要比较Object里面的内容,就只有重写equals()方法了。