StringBuilder
StringBuilder是一个可变的字符串类,我们可以把它看作是一个容器,这里的可变指的是StringBuilder对象中的内容是可变的。
String 和 StringBuilder的区别:
String内容是不可变的
StringBuilder内容是可变的
StringBuilder构造方法
package com.itheima;
public class one_128 {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder();
System.out.println(s1);
System.out.println(s1.length());
StringBuilder s2 = new StringBuilder("abc");
System.out.println(s2);
System.out.println(s2.length());
}
}
StringBuilder添加和反转
package com.itheima;
public class one_129 {
public static void main(String[] args) {
StringBuilder s1 = new StringBuilder();
// StringBuilder s2 = s1.append(“hello”);
//
// System.out.println(s1);
// System.out.println(s2);
// System.out.println(s1 == s2);
s1.append(“hello”);
s1.append(“world”);
s1.append(“ni”).append(1000);
System.out.println(s1);
s1.reverse();
System.out.println(s1);
}
}