为什么程序输出是"不等"?
class P
{
    int cb;
    public static void main(String args[])
    {
     System.out.println("呵呵");   
     P p1=new P();
     P p2=new P();
     p1.cb=1;
     p2.cb=1;
     if(p1.equals(p2))
     {
      System.out.println("相等");
        }
        else
        {
          System.out.println("不等");
        }
    }
}

解决方案 »

  1.   

    int型比较 用 ==     ......................
      

  2.   

    怎么可能相等,又没override equals方法,按Object的equals的话就是比较地址,两个都是new的,肯定是不同的地址,所以肯定不是equal的
      

  3.   

    四楼说得对,equals 必须覆盖,在覆盖的方法里写相应的值比较代码,否则执行的是reference的比较。
      

  4.   

    new 出来的对象equeals肯定不一样的比较分 对象类型 和 基本类型 2个不一样
      

  5.   

    completly agree with  believefym(暮色,miss,迷失,miss)
      

  6.   

    其实默认情况所有的equals方法比较的都是hashcode而这个值是在new 的时候就散列过了,所以相同的概率非常小..也有可能相等:) 我没有见过.谁见过? 可以做个测试,使劲循环创建对象,然后再用equals()方法,看一下是否有可能相等 ??
      

  7.   

    /**
         * Indicates whether some other object is "equal to" this one.
         * <p>
         * The <code>equals</code> method implements an equivalence relation
         * on non-null object references:
         * <ul>
         * <li>It is <i>reflexive</i>: for any non-null reference value
         *     <code>x</code>, <code>x.equals(x)</code> should return
         *     <code>true</code>.
         * <li>It is <i>symmetric</i>: for any non-null reference values
         *     <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
         *     should return <code>true</code> if and only if
         *     <code>y.equals(x)</code> returns <code>true</code>.
         * <li>It is <i>transitive</i>: for any non-null reference values
         *     <code>x</code>, <code>y</code>, and <code>z</code>, if
         *     <code>x.equals(y)</code> returns <code>true</code> and
         *     <code>y.equals(z)</code> returns <code>true</code>, then
         *     <code>x.equals(z)</code> should return <code>true</code>.
         * <li>It is <i>consistent</i>: for any non-null reference values
         *     <code>x</code> and <code>y</code>, multiple invocations of
         *     <tt>x.equals(y)</tt> consistently return <code>true</code>
         *     or consistently return <code>false</code>, provided no
         *     information used in <code>equals</code> comparisons on the
         *     objects is modified.
         * <li>For any non-null reference value <code>x</code>,
         *     <code>x.equals(null)</code> should return <code>false</code>.
         * </ul>
         * <p>
         * The <tt>equals</tt> method for class <code>Object</code> implements 
         * the most discriminating possible equivalence relation on objects; 
         * that is, for any non-null reference values <code>x</code> and
         * <code>y</code>, this method returns <code>true</code> if and only
         * if <code>x</code> and <code>y</code> refer to the same object
         * (<code>x == y</code> has the value <code>true</code>).
         * <p>
         * Note that it is generally necessary to override the <tt>hashCode</tt>
         * method whenever this method is overridden, so as to maintain the
         * general contract for the <tt>hashCode</tt> method, which states
         * that equal objects must have equal hash codes. 
         *
         * @param   obj   the reference object with which to compare.
         * @return  <code>true</code> if this object is the same as the obj
         *          argument; <code>false</code> otherwise.
         * @see     #hashCode()
         * @see     java.util.Hashtable
         */