一.String—>StringBuffer类
方式:
通过有参构造通过无参构造创建字符串缓冲区对象,然后用append追加字符串举例:
public class Demo8 {
public static void main(String[] args) {
String s =
"hello";
StringBuffer sb1 =
new StringBuffer(s);
StringBuffer sb2 =
new StringBuffer();
sb2.append(s);
System.
out.println(
"sb1:"+sb1);
System.
out.println(
"sb2:"+sb2);
}
}
结果:
sb1:hello
sb2:hello
二.StringBuffer—>String类
方式:
有参构造方法toString()方法举例:
public class Demo9 {
public static void main(String[] args) {
StringBuffer sb1 =
new StringBuffer(
"hello");
String s =
new String(sb1);
String s2 = sb1.toString();
System.
out.println(s);
System.
out.println(s2);
}
}
结果:
hello
hello