点击打开链接--------android培训、java培训、期待与您交流!-----------
(1)String:不变类,其内容不可改变
(2)StringBuffer:可变类,其内容可改变,线程安全的
(3)StringBuilder:可变类,非线程安全
String s = "11";
s+="22";
s+="33";
StringBuffer sb = new StringBuffer("11");
sb.append("22");
sb.append("33");
String s = new String(sb);
----------------------
String常量池
----------------------
String s1 = "hello"; //取自常量池
String s2 = "hello"; //取自常量池
System.out.println(s1==s2); //true
String s3 = new String("hello"); //new出新的字符串
String s4 = new String("hello"); //new出新的字符串
System.out.println(s3==s4); //false
String s5 = "hello"; //取自常量池
String s6 = new String("hello"); //new出新的字符串
System.out.println(s5==s6); //false
扩展:直接定义方式和new方式的区别?
String s7 = "hello"; //取自常量池
String s8 = new String("hello"); //new出新的字符串
System.out.println(s7==s8); //false
System.out.println(s7.equals(s8)); //true,因为String类复写了object类中equals方法,该方法、、//用于判断字符串是否相同。
s7和s8的区别:
s7代表一个对象
s8代表两个对象