== 与 equals 第一版

来源:互联网 发布:fc-san网络 编辑:程序博客网 时间:2024/05/21 14:56

上一篇介绍了 关系操作符 和逻辑操作符,现在来分析下 关系操作符中的 ==

Integer i = new Integer(47);Integer j = new Integer(47);Integer k = i;System.out.println("i == j " + (i==j));System.out.println("i != j " + (i!=j));System.out.println("k == i " + (k==i));System.out.println("i.equals(j) " + (i.equals(j)));

输出结果

i == j false
i != j true
k == i true
i.equals(j) true


上面的例子可以知道 == != 比较是引用,而非内容,要想比较2个对象的内容是否相同,我们可以使用 equals()

这个是JDK源码

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

从源码上,可以看到 equals 也是比较的引用,我们上面的例子中 
i.equals(j)
得到的是true 是因为 Integer  中 对equals 进行了覆盖
我们可以查看 intger的源码

再通过一个例子来分析equlas()

    /**     * Compares this object to the specified object.  The result is     * {@code true} if and only if the argument is not     * {@code null} and is an {@code Integer} object that     * contains the same {@code int} value as this object.     *     * @param   obj   the object to compare with.     * @return  {@code true} if the objects are the same;     *          {@code false} otherwise.     */    public boolean equals(Object obj) {        if (obj instanceof Integer) {            return value == ((Integer)obj).intValue();        }        return false;    }


再通过一个例子来分析equlas()

public class TestYSF2 {public static void main(String args[]){TestA a = new TestA();TestA b = new TestA();a.value = b.value = 10;System.out.println(" a.equals(b) " + (a.equals(b)));}}class TestA{int value ;}


运行结果是
a.equals(b) false
根据JDK的源码可以知道a.equals(b) 比较的是a,b2个对象的引用是否相同