equals 与 ==

引申出的知识点:

1 int与integer的比较 Integer vs int

string的比较String常量池 及 String StringBuilder StringBuffer 对比 总结得非常好 包括string.intern

 

2 因为==常常决定锁的有效性,进而再引出以integer或string作为锁

Integer String 对象作为锁的注意事项

Integer vs int

由一个多线程共享Integer类变量问题引起的。。。

 

3 ==是public static native int identityHashCode(Object x);相等的充分非必要条件

hashcode & System.identityHashCode

 

4 如果某个类是单例,则可以直接用==判断是否相同,比如jvm方法区对于Class类的对象(类对象)

同一个类加载器下某个全类名类只能由一份

String s = "x";
String l = "x";
System.out.println(s.getClass() == l.getClass());

注意,不同类加载器下,即使是同一个类,也会==相同,因为在方法区中内存地址不同

String s = "x";
String l = "x";
System.out.println(s.getClass() == l.getClass());

posted on 2019-12-11 10:58  silyvin  阅读(270)  评论(0编辑  收藏  举报