代码注释如下:
package demo06StringBuilder;
/*
StringBuilder和String可以相互转换:
String->StringBuilder:可以使用StringBuilder的构造方法
StringBuilder(String str) 构造一个字符串生成器,并初始化为指定的字符串内容。
StringBuilder->String:可以使用StringBuilder中的toString方法
public String toString():将当前StringBuilder对象转换为String对象。
*/
public class Demo03StringBuilder {
public static void main(String[] args) {
//String->StringBuilder
String str = "hello";
System.out.println("str:"+str);//str:hello
StringBuilder bu = new StringBuilder(str);//String->StringBuilder,将string型的str转换成StringBuilder型的bu
//往StringBuilder中添加数据
bu.append("world");//"hello"+"world"
System.out.println("bu:"+bu);//bu:helloworld
//StringBuilder->String
String s = bu.toString();//对StringBuilder型的bu使用toString方法来转换成string型的s
System.out.println("s:"+s);//s:helloworld
}
}
版权声明:本文为xky1214原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。