我的理解:当a, b指向同一对象实例时,a==b一定返回true;
当a, b的各字段分别相等时,a.equals(b)一定返回true;

解决方案 »

  1.   

    回答:
    ( 1 )
    用 a.equals(b) 来进行判断a与b的内容是否相同,相同是true,相反是false.
    用 a==b 来进行判断a, b是否指向同一对象,是返回true,不是返回false.
      

  2.   

    object的默认equals实现:a.equals(b)和a==b有一样的结果,即默认实现中认为只有a和b指向同一个对象时才返回true。
    子类可以重写equals方法,比如String,就重写了equals,认为只要String的字符系列相同就可以返回true。
      

  3.   

    a.equals(b)用来比较两个对象的内容是否相同
    a==b用来比较是否同指一个对象!
      

  4.   

    楼上各位说的都对,补充一点就是调用equals的方法的类一定要覆写equals这个method的,不然你调用equals的时候默认的还是通过==比较
      

  5.   

    (1)不用使用==运算符来检测两个字符串是否相等!他只是测定两个串是否存储在同一个位置。
    (2)在同一内存块上当然,都是true了。当a ,b 指向不同内存块,但相等时,a.equals(b)返回true;a==b返回false.
      

  6.   

    看equals这个方法在a这个对象中怎么定义的啰,如果定义的是a==b,那就是一样的,如果不是就要看具体情况了
      

  7.   

    默认equals()方法比较对象句柄是否指向同一地址.
    如果想比较对象内容就必须重写equals().
    java基本型别已经重写了equals().如果想比较自己的CLASS,就必须自己重写.
      

  8.   

    当a ,b 指向同一个内存块时,a.equals(b) 与 a==b 分别返回(true or false)用a.equals(b)此时用来比较a和b是不是指向同一个对像 (指向同一个内存块)应用于比较对象时
    用a==b 它俩是不同的引数 (变量)  (a和b是它们指向对像的一个引用而已,如果对象是电视机 a和b 就是摇控器(java编程思想) a和b 都可以控制同一台电视,但它们是不同的摇控器)
      

  9.   

    更具我的经验,判断是否为null用==,判断字符选想等用equals
      

  10.   

    判断内容相等用epuals()
    判断是否指向同一个对象用==
      

  11.   

    具体情况具体分析
    2:很大可能true true
    true false
      

  12.   

    a.equals(b)比较的是字符串的值是否相等
    a==b比较的是a和b的地址是否相等
    两者是有区别的
      

  13.   

    equals()是object定义的一个接口!目的就是通过单根继承解决java不支持运算符重载。
    例如:
         正如大家所说的==只是判断实力的地址是否相等,如果我们想比较实力的实际内容是否相等,我们可以使用equals()(因为单根继承所以每一个对象都有),而C++使用运算符重载。
      

  14.   

    测试一下下面的代码,看一下所得的结果与自己想象的有什么不同,然后看看API,和Object、String及StringBuffer这三个类的源代码,着重看一看其中是否重写了equals()方法,以及重写次方法的类中equals()所比较的是什么,是reference还是value?可以在自己的类中重写equals()方法,按照自己的意愿去比较两个类是否相等(同)!public class TestEquals{
     public TestEquals(){}
     public void testEquals(){
      String testA = "abc";
      String testB = "abc";
      String testC = new String("abc");
      String testD = new String("abc");
      StringBuffer testE = new StringBuffer("abc");
      StringBuffer testF = new StringBuffer("abc");
      System.out.println("\"abc\" == \"abc\" is: "+ (testA == testB));
      System.out.println("\"abc\" equals \"abc\" is: "+ (testA.equals(testB)));
      System.out.println("new String(\"abc\") == new String(\"abc\") is: "+ (testC == testD));
      System.out.println("new String(\"abc\") equals new String(\"abc\") is: "+ (testC.equals(testD)));
      System.out.println("new StringBuffer(\"abc\") == new StringBuffer(\"abc\") is: "+ (testE == testF));
      System.out.println("new StringBuffer(\"abc\") equals new StringBuffer(\"abc\") is: "+ (testE.equals(testF)));
      System.out.println("\"abc\" == new String(\"abc\") is: "+ (testA == testC));
      System.out.println("\"abc\" equals new String(\"abc\") is: "+ (testA.equals(testC)));
      System.out.println("\"abc\" equals new StringBuffer(\"abc\") is: "+ (testA.equals( testE)));
      System.out.println("new String(\"abc\") equals new StringBuffer(\"abc\") is: "+ (testC.equals( testE)));
     }
     public static void main(String[] args){
      TestEquals test = new TestEquals();
      test.testEquals();
     }
    }
      

  15.   

    a==b是检测a和b这两个变量是否指向一个对象
    a.equals(b)是检测这两个对象内容是否相等,a和b如果是两个对象但是内容一样,此时返回true,但是这种情况如果使用a==b就返回false
      

  16.   

    这个问题在thinking in java里有比较详细地说明
      

  17.   

    public class PersonEquals{
     private String name = null;
     private String address = null;
     private String age = null;
     public PersonEquals(){}
     public void setName(String name){
      this.name = name;
     }
     public String getName(){
      return name;
     }
     public void setAddress(String address){
      this.address = address;
     }
     public String getAddress(){
      return address;
     }
     public void setAge(String age){
      this.age = age;
     }
     public String getAge(){
      return age;
     }
     public boolean equals(Object aObject){
      boolean theSame = false;
      if(aObject instanceof  PersonEquals){
       if(this.name == ((PersonEquals)aObject).getName()){
        theSame = true;
       }
      }
      return theSame;
     } public String toString(){
      return "["+name+","+address+","+age+"]";
     } public static void printResult(PersonEquals personA, PersonEquals personB, boolean result){
      if(result){
       System.out.println("The person"+personA.toString()+" and the person"+personB.toString()+" is the same!");
       System.out.println("Because their names are 'equals' under my control:) ");
      }else{
       System.out.println("The person"+personA.toString()+" and the person"+personB.toString()+" is not the same!");
       System.out.println("Because their names are not 'equals' under my control:) ");
      }
     } public static void main(String[] args) {
      PersonEquals testA = new PersonEquals();
      testA.setName("abc");
      testA.setAddress("beijing");
      testA.setAge("80");
      PersonEquals testB = new PersonEquals();
      testB.setName("abc");
      testB.setAddress("shanghai");
      testB.setAge("8");
      PersonEquals testC = new PersonEquals();
      testC.setName("def");
      testC.setAddress("beijing");
      testC.setAge("80");
      Object notPerson = new String("abc");
      printResult(testA, testB, testA.equals(testB));
      printResult(testA, testC, testA.equals(testC));
      printResult(testC, testB, testC.equals(testB));
      System.out.println("The same of person"+testA.toString()+" and "+notPerson.toString()+" is "+testA.equals(notPerson));
     }
    }
      

  18.   

    对于字符串应该先用==看看是不是null在用equals,否则会有异常。
      

  19.   

    看看object类的api
    很多人把某个类的equals方法泛化了,有误导作用
      

  20.   

    a.equals(b)  比较a,b的内容
    a == b            比较a,b的instance指向的是否是同一个对象例子:
    1. 
    a = "aaa";
    b = "aaa";
    a.equals(b)   //true
    a == b            // true2. 
    a = "aaa";
    b = new String("aaa");
    a.equals(b)  // true
    a == b           // false
      

  21.   

    a = "aaa";
    b = "aaa";为什么a == b // true呢?
      

  22.   

    若想比较两个对象的实际内容是否相同,必须使用所有对象都适用的特殊方法equals().但这个方法不合适用于“主类型”,那些类型直接使用==和!=即可。例:
    public class EqualsMethod{
      public static void main(String[] args){
      Integer n1=new Integer(47);
      Integer n2=new Integer(47);
      System.out.println(n1.equals(n2));
    }
    }
    结果为true.
    equals()的默认行为是比较句柄。
      

  23.   

    equals是判断两边的对象(引用)是否相等的,值为true,false;
    而==是判断基本数据类型是否想等的
      

  24.   

    String i=.........先检查内存中是否开辟了内存(i是否被赋值)如果没有,则开辟,如果有了,则用相同的内存 ,这就是跟StringBuffer不同的地方
    例如String i="hello world";
    然后你又有了String d="hello world";他们的内存是相同的 
    说白了就是 equals在内存相同是比较的都是true,但是在内存空间不同时则是比较的值
      

  25.   

    上面说的都对,补充一点,在实际编程时,对于字符串应该先用==看看是不是null再用equals,
    否则会有异常
      

  26.   

    a.equals(b)用在比较两个实例是否相等
    a==b 比较对象是否相等
    在java中,类的实例都被重载,例如:
    Integer i=new Integer(23);
    Integer s=new Integer(23);
    当用i.equals(s)时, true
    用i==s时,          false
    当你创建用户自定义类时,你的输出就不一样啦
    例如
    aa a=new aa("hh")
    aa b=new aa(hh")
    用a.equals(b)时,    为false