package com.demo3;public class StringBuilderDemo1 {public static void main(String[] args) {// 创建一个空白可变字符串对象,不含有内核内容StringBuilder s = new StringBuilder();System.out.println("s:"+s.length());// 根据字符串内容,来创建可变字符串对象StringBuilder s1 = new StringBuilder("haha");System.out.println("s:"+s1.length());}
}
s:0
s:4
package com.demo3;public class SBDemo2 {public static void main(String[] args) {StringBuilder sb = new StringBuilder();// StringBuilder sb2 = sb.append("hehe");// 字符串添加StringBuilder sb2 = sb.append("hehe").append("嘟嘟嘟").append(19099);System.out.println("sb:" + sb);System.out.println("sb2:" + sb2);// 字符串反转sb.reverse();System.out.println("sb:" + sb);System.out.println(sb == sb2);}
}
sb:hehe嘟嘟嘟19099
sb2:hehe嘟嘟嘟19099
sb:99091嘟嘟嘟eheh
true
package com.demo3;/*** StringBuilder 转 String : toString* String 转 StringBuilder*/
public class Demo3 {public static void main(String[] args) {/*StringBuilder sb = new StringBuilder();sb.append("hehe");String s = sb.toString();System.out.println(s);*/// String 转 StringBuilderString s = "hello";// StringBuilder sb = s; // 错误的StringBuilder sb = new StringBuilder(s);System.out.println(sb);}
}
hello