JAVA equals和==的區別

一句話:java

==比較的是內存地址,若是內存地址相同,則相同;ide

equals 內存地址相同則相同,若是內存地址不相同,看子類equals重寫方法的具體實現。this

package JavaCoreTechDemo.base;

import sun.misc.Unsafe;

import java.lang.reflect.Field;

/**
 * Created by IntelliJ IDEA.
 * User: zz
 * Date: 2015/5/10
 */
public class JavaEqualsAndDoubleEqual {
    public static void main(String[] args) throws Exception {
        testInt();
        testNotOverrideEquals();
        testStringEquals();
        testNewStringEquals();
    }

    private static void testInt() {
        System.out.println("=====================testInt=========================");
        int a = 1;
        int b = 1;
        System.out.println(String.format("a mem addr=%s,b mem addr=%s", AddressPrint.addressOf(a), AddressPrint.addressOf(b)));
        if (a == b) {
            System.out.println("a==b");
        }
        Integer c = new Integer(a);
        Integer d = new Integer(b);
        System.out.println(String.format("c mem addr=%s,d mem addr=%s", AddressPrint.addressOf(c), AddressPrint.addressOf(d)));

        if (c == d) {
            System.out.println("Integer  c  == Integer  d");
        }
        System.out.println("this.value=" + c.intValue());
        System.out.println("(Integer)var1=" + (Integer) d);

        if (c.equals(d)) {
            System.out.println("Integer  c  equals Integer  d");
        }

    }

    private static void testNotOverrideEquals() {
        System.out.println("=====================testNotOverrideEquals=========================");
        TestA testA1 = new TestA();
        TestA testA2 = new TestA();
        System.out.println(String.format("testA1 mem addr=%s,testA2 mem addr=%s", AddressPrint.addressOf(testA1), AddressPrint.addressOf(testA2)));
        if (testA1 == testA2) {
            System.out.println("TestA testA1  == TestA testA2");
        } else {
            System.out.println("TestA testA1 != testA2");
        }

        if (testA1.equals(testA2)) {
            //默認實現的equals比較的是對象的內存地址
            System.out.println("TestA testA1  equals testA2");
        } else {
            System.out.println("TestA testA1 not equals testA2");
        }
    }

    private static void testStringEquals() {
        System.out.println("=====================testStringEquals=========================");
        String s1 = "Monday";
        String s2 = "Monday";
        System.out.println(String.format("s1 mem addr=%s,s2 mem addr=%s", AddressPrint.addressOf(s1), AddressPrint.addressOf(s2)));
        //==比較的是內存地址,s1和s2內存地址同樣
        if (s1 == s2) {
            System.out.println("s1 == s2");
        } else {
            System.out.println("s1 != s2");
        }
        if (s1.equals(s2)) {
            System.out.println("s1 equals s2");
        } else {
            System.out.println("s1 not equals s2");
        }
    }

    private static void testNewStringEquals() {
        System.out.println("=====================testNewStringEquals=========================");
        String s1 = "Monday";
        String s2 = new String("Monday");
        //==比較的是內存地址,s1和s2內存地址不同
        System.out.println(String.format("s1 mem addr=%s,s2 mem addr=%s", AddressPrint.addressOf(s1), AddressPrint.addressOf(s2)));

        if (s1 == s2) {
            System.out.println("s1 == s2");
        } else {
            System.out.println("s1 != s2");
        }
        //String類重寫了equals,比較的是每一個字符
        if (s1.equals(s2)) {
            System.out.println("s1 equals s2");
        } else {
            System.out.println("s1 not equals s2");
        }
    }
}

class TestA {

}

class AddressPrint {
    private static Unsafe unsafe;

    static {
        try {
            Field field = Unsafe.class.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            unsafe = (Unsafe) field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String addressOf(Object o) {
        Object[] array = new Object[]{o};

        long baseOffset = unsafe.arrayBaseOffset(Object[].class);
        int addressSize = unsafe.addressSize();
        try {

            long objectAddress;
            switch (addressSize) {
                case 4:
                    objectAddress = unsafe.getInt(array, baseOffset);
                    break;
                case 8:
                    objectAddress = unsafe.getLong(array, baseOffset);
                    break;
                default:
                    throw new Error("unsupported address size: " + addressSize);
            }

            return (Long.toHexString(objectAddress));
        } catch (Exception e) {
            System.out.println("unsupported address size:" + addressSize);
            return null;
        }
    }

}
相關文章
相關標籤/搜索