==比较的时对象所在的内存地址是否相同
equals比较的时内存地址中的内容,只要内容相同就返回true,而不考虑比较的内容是否在同一个内存地址中.

解决方案 »

  1.   

    找到一个解释equals和==的,不过不尽如人意,希望有帮助吧。:)equals方法是Object类的一个方法,所有继承自Object类的类都会集成此方法,并且可以重载这个方法来实现各自的比较操作,而且jdk也正是推荐这种做法。所以开发人员尽可以在自己的类中实现自己的equals方法来完成自己特定的比较功能,所以各个类的equals方法与= =之间并没有绝对的关系,这要根据各自类中自己的实现情况来看。也就是说可能会有两种情况发生:equals方法和= =相同或者不相同。在多数情况下这两者的区别就是究竟是对对象的引用进行比较还是对对象的值进行比较(其他特殊情况此处不予考虑)。那么= =操作符是比较的什么呢?= =操作符是比较的对象的引用而不是对象的值。并且由下面的源代码可以看出在最初的Object对象中的equals方法是与= =操作符完成功能是相同的。 
    源码:
    java.lang.Object.equals()方法:
    -------------------------------------------------------------
    public boolean equalss(Object obj) {
     return (this = = obj);
        }
    -------------------------------------------------------------
    jdk文档中给出如下解释: 
    -------------------------------------------------------------
    The equalss method implements an equivalence relation: 
    · It is reflexive: for any reference value x, x.equalss(x) should return true. 
    · It is symmetric: for any reference values x and y, x.equalss(y) should return true if and only if y.equalss(x) returns true. 
    · It is transitive: for any reference values x, y, and z, if x.equalss(y) returns true and y.equalss(z) returns true, then x.equalss(z) should return true. 
    · It is consistent: for any reference values x and y, multiple invocations of x.equalss(y) consistently return true or consistently return false, provided no information used in equalss comparisons on the object is modified. 
    · For any non-null reference value x, x.equalss(null) should return false. 
    The equalss 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). 
    Note that it is generally necessary to override the hashCode method whenever this method is overridden, so as to maintain the general contract for the hashCode method, which states that equals objects must have equals hash codes. 
    -------------------------------------------------------------
    由以上的注释可知equals方法和 = =操作符是完成了相同的比较功能,都是对对象的引用进行了比较。那么我们熟悉的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;
        }-------------------------------------------------------------
    此方法的注释为:
    -------------------------------------------------------------
    Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
    -------------------------------------------------------------
    由上面的代码和注释可以得到String类的equal方法是对对象的值进行比较。
    根据以上的讨论可以得出结论:equal方法和= =操作符是否存在区别要个别对待,要根据equal的每个实现情况来具体判断。
    *******************************
      

  2.   

    ==是比较对象
    equals()比较字符串才正确
      

  3.   

    ==要类型一样,例如两者都为对象型或字符串型
    equals应该是都转换成简单数据类型来比较
      

  4.   

    ==是比较对象
    equals()比较字符串才正确
    这才说的正确
      

  5.   

    ==比较内存地址,equals比较内存内容。
      

  6.   

    首先:==比较的时对象所在的内存地址
       所以前面三个都好理解主要是为什么s1==s2 为trueString s1 = "ValidateException";
    String s2 = "ValidateException";
    编译时只产生了一个对象"ValidateException",所以s1==s2 true
    如果:
    String s1 = new String("ValidateException");
    String s2 = new String("ValidateException");
    则s1==s2 false
    因为s1,s2在运行时才产生对象,两个对象!!oK!
      

  7.   

    object里的equals也是比较内存 但你继承他并复写了就是比较内容了比如string里的equals...
      

  8.   

    ==比较地址
    EQUALS 是正确字符串比较
      

  9.   

    以前看到过专门的文章,没找到,说个大概吧。
    equal是比较内容,只要两个字符串内容一样就是true
    ==是比较引用,只有指向同一引用时才是true
    String 这个东西比较奇怪,它有一个缓冲池,当你用以下方式同赋值时,
    String s1 = "ValidateException";
    String s2 = "ValidateException";
    第一句会先new 一个"ValidateException",当第二句执行时,它会先到String的缓冲池中去看一下,有没有同样的String,如果有,就将这个String的引用赋给第二个String,这样,用==就会得到true的值。但如果你是这样写的话
    String s1 = new ("ValidateException");
    String s2 = new ("ValidateException");
    用equal会得到true用new 会得到false。因为它生成了两个引用。你的问题也一样,
    String s = request.getParameter("excType");
    这句话会得到一个新的引用,所以是false.
      

  10.   

    一句话:"=="比较的是句柄,而equals()才是比较内容的.你这里出现s1==s2为真是时,是因为它们是同一引号,即句柄相同,而S则不同,
    <Think in java>的
    第3章 控制程序流程
    有详细讲解.
      

  11.   

    http://www.chinajavaworld.net/forum/topic.cgi?forum=20&topic=16630&show=0
    这个地方有详细的说明!!!