之乎者也,阿弥陀佛

软件设计的原则就是,化繁为简,化难为易,把人的思维集中在简单的领域,然后通过有序的组合实现复杂的逻辑。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一句话:

==比较的是内存地址,如果内存地址相同,则相同;

equals 内存地址相同则相同,如果内存地址不相同,看子类equals重写方法的具体实现。

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;
        }
    }

}

  

posted on 2015-05-10 11:40  搏击的小船  阅读(212)  评论(0编辑  收藏  举报