本帖最后由 谢毅 于 2013-1-23 15:23 编辑
StringBuffer是一个容器,里面是按索引值存放字符的,我们都知道字符都在字符常量区,不能修改,能改变的只能是指向他们的内存地址值,所以Buffer就是按索引值去替换掉内存地址值。- public class Str1 {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- StringBuffer s1=new StringBuffer("abcdefghik");
-
- System.out.println(s1.hashCode());
- s1=s1.replace(1,4, "ada");
- System.out.println(s1);
- System.out.println(s1.hashCode());
-
- System.out.println("---------------------");
- String s2="abcdefghik";
- //System.out.println(s1.contains("sad"));
- System.out.println(s2.hashCode());
- s2=s2.replace("cd", "ada");
- System.out.println(s2);
- System.out.println(s2.hashCode());
- }
- }
复制代码 结果为:- 7971559
- aadaefghik
- 7971559
- ---------------------
- -634317658
- abadaefghik
- 1846299905
复制代码 |