==对基本数据类型而言是比较值,对于对象而言是比较对象的地址。
equals是比较两个对象的值最近遇到这样一个问题,有一个sql.Date类的对象,欲判断该对象是否为空使用以下的语句:
if(!oDate.equals(null))
…………每次执行到这个地方都会抛出一个异常。但是将该语句改成:
if(!(oDate==null))
…………则一切皆正常。请问如果对象和null进行比较的时候为何是使用==,而String型的对象则可以使用equals(null)进行比较?

解决方案 »

  1.   

    oDate.equals(xxx)
    如果oDate是null, 就相当于null.equals(xxx) ,当然会异常
      

  2.   

    就如楼主所言,==一般是对象的比较,也就是内存地址的比较
    equals是值的比较,如果对象都不存在,何来的内容呢?
      

  3.   

    String 的equals方法的原码:
    public boolean equals(Object anObject) {
    if (this == anObject) {
        return true;
    }
      

  4.   

    帖错了 !应该是这个 !
    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;
        }
      

  5.   

    String 是final 类的,
    #  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;
    #     }
      

  6.   

    如此说来null也是一个常量啦?
      

  7.   

    ==表示句柄的比较,确定是不是同一个对象实例。equals是方法,这个方法是从根对象(Object)开始的,他的实现就是==,这个方法可以被覆盖,比如大家熟悉的String就是覆盖了的方法equals
    public boolean equals(Object obj)Indicates whether some other object is "equal to" this one. 
    The equals method implements an equivalence relation on non-null object references: It is reflexive: for any non-null reference value x, x.equals(x) should return true. 
    It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true. 
    It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true. 
    It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified. 
    For any non-null reference value x, x.equals(null) should return false. 
    The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null 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 equal objects must have equal hash codes. 
    Parameters:
    obj - the reference object with which to compare. 
    Returns:
    true if this object is the same as the obj argument; false otherwise.
    See Also:
    hashCode(), Hashtable
      

  8.   

    java.sql.Date 继承的是java.util.Datejava.util.Date覆盖了Object.equals(Object obj)方法/**
         * Compares two dates for equality.
         * The result is <code>true</code> if and only if the argument is 
         * not <code>null</code> and is a <code>Date</code> object that 
         * represents the same point in time, to the millisecond, as this object.
         * <p>
         * Thus, two <code>Date</code> objects are equal if and only if the 
         * <code>getTime</code> method returns the same <code>long</code> 
         * value for both. 
         *
         * @param   obj   the object to compare with.
         * @return  <code>true</code> if the objects are the same;
         *          <code>false</code> otherwise.
         * @see     java.util.Date#getTime()
         */
        public boolean equals(Object obj) {
            return obj instanceof Date && getTime() == ((Date) obj).getTime();
        }
    你就知道为什么null不能比较了吧
      

  9.   

    http://blog.csdn.net/hekai1112004/archive/2005/08/12/452569.aspx