字符串String             集合Collection
异常相关exception        设计模式 Design Pattern
反射相关 Reflection      相等性 equals hashCode
线程相关 Thread
多态 Polymorphism
数组Array

字符串:代码如下

String s = new String("abc");

String s1 = "abc";

String s2 = new String("abc");

system.out.println(s == s1);

system.out.println(s == s2);

system.out.println(s1 == s2);

system.out.println(s == s.intern());

 

String lo = "lo";

hello == "hel" + lo;    (直接在堆中生成两个对象 所以==左侧是Stringpool中 右侧是堆中所以不相同)

第一行代码生成两个对象 s引用和 abc对象,首先在string pool中查找有无abc对象没有则把abc对象放入池中,接着再new生成abc对象是在heap中。

第二行是首先在string pool中查找有无abc对象 有就不会生成 直接使用所以S1和S指向了不同的对象

第三行也是生成新的对象。

==对于非源生数据类型 比较的是引用的地址也就是说是否是指向了同一个对象

intern()方法首先检查stringpool中是否有abc对象如果有返回池中abc的地址

s.intern() == t.intern() is true if and only if s.equals(t) is true.

posted on 2011-07-26 16:19  阳光银行  阅读(260)  评论(0)    收藏  举报