When you use == with a primitive -int, double, char, ... you are checking that the values
are identical. But if you use == with an object, you are checking that the 2 objects are stored at the
same address. In other words the references pointing to the same object...
Method equals () is different.
It is the same as ==, if it isn't overriden by the object class.
Many classes override the method equals (). In this case this method will check that content of the
object is the same or not, not addresses.

解决方案 »

  1.   

    所以,尽量使用equals来比较对象的属性值。
      

  2.   

    String 对象比较时,除了和null可以用==,其他的都最好用equals   要不会包错
      

  3.   

    skyyoung(路人甲),我好崇拜你!
      

  4.   

    skyyoung(路人甲) 兄 very good !!!
      

  5.   

    对于字符串,==比较的不是内容,而是内存地址。
    而"equals" 比较内容。例如:String a="dddd";
          String b=new String(a.getBytes());
     这时 (a.equals(b))为true;
       但 (a==b)为false.
      

  6.   


    String 有两种创建形式1。  String a = "dddd";
         String b = "dddd";
      这时候 (a==b)和(a.equals(b)) 都返回true
      
      因为在执行String a = "dddd";  时,编译器会创建一个String对象"dddd",放
      入一个pool中,当继续执行到  String b = "dddd";时,编译器会自动将引用b
      指向pool中的对象"dddd",也就是说,和引用a指向的对象时一样的。2。  String a = "dddd";
         String b = new String("dddd");
      这时只有 b.equals(a)才会返回true;
      在执行String a = "dddd"; 时,同上面一样,编译器会创建一个String对象"dddd"放
      入pool中,但时当继续执行到  String b = new String("dddd");时,则会产生一个
      新的String对象,因此引用a与引用b指向不同的对象,但是两个对象所包含的内容时一样
      的。再补充一点, 如果还有 String c = new String("xxxx"); 则实际上会产生两个对象,
    一个在pool中,另一个在程序空间中,c指向程序空间中的那一个。假设再有
    String d = String("xxxx"); 则d会直接指向pool中的"xxxx"String对象,省去了新建
    对象这一过程
      
      
      
      

  7.   

    if (e.getActionCommand == "Exit")
    {
        dispose ();
        System.exit(0);
    }
      

  8.   

    有道理,以前我比较字符串就惨了
    if(a.compareTo(b)),现在感觉非常笨