前言
String、StringBuilder、StringBuffer皆是用来存储字符串类型的数据的
主要区别在于:
- String是不可变的,另外两个是可变的字符串
- String可以通过字面量定义也可以通过
new
关键字定义,另外两个只能通过new
来创建对象使用场景
- 在定义一个不需要频繁更改的字符串的时候,可以使用String,如果需要频繁更改,则考虑其他两个,性能要好
- 如果定义需要频繁更改的字符串,如果不考虑线程安全问题,建议使用
StringBuilder
,反之则用StringBuffer
1、String
特点:
String是一个
final
类,代表不可变的字符序列,不可被继承,下面看源码的时候也能看到String实现了
Serializable
接口,:表示String是支持序列化的String实现了
Comparable
接口,表示String是可以比较大小的通过
new
关键字带参创建对象,创建的是两个对象,一个是堆空间中new结构,另一个是char[]对应的常量池中的数据:abc
String s = new String("abc") // 创建了两个对象
String对象的字符内容是存储在一个字符数组
final char value[]
中的下面看源码也可以看到String代表不可变的字符序列。简称:不可变性。体现:
- 当对字符串重新赋值时,需要重新指定内存区域赋值,不能使用原有的value进行改值
- 当对现有的字符串进行拼接操作时,也需要重新指定内存区域赋值。
1.1、查看不可变特点
String s1 = "hello";
String s2 = "hello";
s1 = "world";
System.out.println(s1 == s2); // false 比较s1和s2的地址值
String s3 = "hello";
String s4 = "hello";
s3 += "world";
System.out.println(s3 == s4); // false 当对现有地址值进行连接操作时,也需要重新指定内存区域赋值
1.2、构造器
(1)、字面量创建方式
String str = "hello"; // 此时是在常量池中创建了一个对象(数据)
(2)、new
关键字创建
// 本质上为:this.value = new char[0]
String str1 = new String();
// this.value = original.value
String str2 = new String(String original);
// this.value = Arrays.copyOf(value,value.length)
String str3 = new String(char[] a);
1.3、常用方法
(1)、length()
int length():返回字符串的长度(返回的是存放所有字符的value数组的长度)
public static void main(String[] args) {
String str = "hello";
System.out.println(str.length()); // 5
}
(2)、charAt(int index)
char charAt(int index):返回某索引处的字符,下标从0开始
public static void main(String[] args) {
String str = "hello";
char c = str.charAt(1);
System.out.println(c); // e
}
(3)、isEmpty()
boolean isEmpty():判断是否为空字符串(就是判断value.length == 0)
public static void main(String[] args) {
String str = "hello";
boolean b = str.isEmpty();
System.out.println(b); // false
str = "";
System.out.println(str.isEmpty()); // true
}
(4)、toLowerCase()
String toLowerCase():使用默认语言环境,将String中的所有字符转换为小写
public static void main(String[] args) {
String str = "HELLo";
String s = str.toLowerCase();
System.out.println(s); // hello
}
(5)、toUpperCase()
String toUpperCase():使用默认语言环境,将String中的所有字符串转换为大写
public static void main(String[] args) {
String str = "hellO";
String s = str.toUpperCase();
System.out.println(s); // HELLO
}
(6)、trim()
String trim():返回字符串的副本,忽略前导空白和尾部空白,不能去除中间空格
public static void main(String[] args) {
String str = " hello world ";
String trim = str.trim();
System.out.println(trim); // hello world
}
(7)、equals(Object obj)
boolean equals(Object obj):比较字符串的内容是否相同,比较的为内容,参数一般也为字符串,否则比较没意义
public static void main(String[] args) {
String str = "hello world";
String str2 = "hello world";
boolean equals = str.equals(str2);
System.out.println(equals); // true
System.out.println(str.equals("Hello World")); // false 不忽略大小写
}
(8)、equalsIgnoreCase(String anotherString)
boolean equalsIgnoreCase(String anotherString):区别于上方,比较是否相同,但忽略大小写
public static void main(String[] args) {
String str = "hello world";
String str2 = "hello world";
boolean equals = str.equalsIgnoreCase(str2);
System.out.println(equals); // true
System.out.println(str.equalsIgnoreCase("Hello World")); // true 忽略大小写
}
(9)、concat(String str)
String concat(String str):将指定字符串链接到此字符串的结尾。等价于用:“+”
public static void main(String[] args) {
String str = "hello";
String s = str.concat(" world"); // 不改变自身,重新返回一个字符串
System.out.println(str); // hello
System.out.println(s); // hello world
// 字符串拼接会改变自己的值
str += " world";
System.out.println(str); // hello world
}
(10)、compareTo(String anotherString)
int compareTo(String anotherString):比较两个字符串大小,比较的为字符串的
ASCII
码值,如果第一位相同,就比较第二位的
public static void main(String[] args) {
String str1 = "a"; // 在ASCII码中为97
String str2 = "b"; // 在ASCII码中为98
String str3 = "c"; // 在ASCII码中为99
int i = str1.compareTo(str2);
System.out.println(i); // -1 97-98=-1
System.out.println(str1.compareTo(str3)); // -2 97-99=-2
String str4 = "aa";
String str5 = "ac";
System.out.println(str4.compareTo(str5)); // -2 如果第一位相同就比较第二位的 97-99=-2
}
(11)、subString(int beginIndex)
String subString(int beginIndex):返回一个新的字符串,它是此字符串的从beginIndex开始截取到最后一个子字符串
public static void main(String[] args) {
String str = "hello world!";
String s = str.substring(4);
System.out.println(s); // o world! 从下标为4开始(包括4),截取后面的字符串
}
(12)、subString(int beginIndex, int endIndex)
String subString(int beginIndex, int endIndex):返回一个新的字符串,它是此字符串从
beginIndex
开始截取到endIndex
(左闭右开)的一个子字符串
public static void main(String[] args) {
String str = "hello world!";
String s = str.substring(6, 11);
System.out.println(s); // world 从下标为6(w)开始,截取到下标为11(!)的字符串,左闭右开原则
String sub = str.substring(6, 12);
System.out.println(sub); // world! 要取到最后一位就让最后一位的下标+1,不会出现下标越界。
String sub2 = str.substring(6, 13); // 异常:StringIndexOutOfBoundsException
System.out.println(sub2);
}
(13)、endsWith(String suffix)
boolean endsWith(String suffix):测试此字符串是否以指定的后缀结束
public static void main(String[] args) {
String str = "hello world!";
boolean b1 = str.endsWith("rld!");
System.out.println(b1); // true
boolean b2 = str.endsWith("rld");
System.out.println(b2); // false
}
(14)、startsWith(String prefix)
boolean startsWith(String prefix):测试此字符串是否以指定的前缀开始
public static void main(String[] args) {
String str = "hello world!";
boolean b1 = str.startsWith("h");
System.out.println(b1); // true
boolean b2 = str.startsWith("ello");
System.out.println(b2); // false
}
(15)、startsWith(String prefix, int toffset)
boolean startsWith(String prefix, int toffset):测试此字符串从指定索引开始的子字符串是否以指定前缀开始
public static void main(String[] args) {
String str = "hello world!";
boolean b1 = str.startsWith("o",4);
System.out.println(b1); // true
boolean b2 = str.startsWith(" ",5);
System.out.println(b2); // true
}
(16)、contains(CharSequence s)
boolean contains(CharSequence s):当且仅当此字符串包含指定的char值序列时,返回true
public static void main(String[] args) {
String str = "hello world!";
boolean b1 = str.contains("llo");
System.out.println(b1); // true
boolean b2 = str.contains("a");
System.out.println(b2); // false
}
(17)、indexOf(String str)
int indexOf(String str):返回指定子字符串在此字符串中第一次出现处的索引,如果没有则返回-1
public static void main(String[] args) {
String str = "hello world!";
int i1 = str.indexOf(" ");
System.out.println(i1); // 5
int i2 = str.indexOf("llo");
System.out.println(i2); // 2
int i3 = str.indexOf("a");
System.out.println(i3); // -1
}
(18)、indexOf(String str, int fromIndex)
int indexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始,如果没有则返回-1
public static void main(String[] args) {
String str = "hello world!";
int i1 = str.indexOf("o",4);
System.out.println(i1); // 4 第一个o,下标为4,从4开始(包含4)
int i2 = str.indexOf("o",5);
System.out.println(i2); // 7 第二个o,下标为7,从下标为5开始,就不包含第一个o了
int i3 = str.indexOf("o",7);
System.out.println(i3); // 7 第二个o,下标为7,从7开始(包含7)
int i4 = str.indexOf("o",8);
System.out.println(i4); // -1 第二个o,下标为7,从7以后无o了,结果为-1
}
(19)、lastIndexOf(String str)
int lastIndexOf(String str):返回指定子字符串在此字符串中最右边(后边)出现处的索引,如果没有则返回-1
public static void main(String[] args) {
String str = "hello world!";
int i1 = str.lastIndexOf("o");
System.out.println(i1); // 7 最后面开始数,第一次出现的索引位置
int i2 = str.indexOf("a");
System.out.println(i2); // -1 如果不存在,则返回-1
}
(20)、lastIndexOf(String str, int fromIndex)
int lastIndexOf(String str, int fromIndex):返回指定子字符串在此字符串中最后一次出现处的索引,从指定的索引开始返向搜索,如果没有则返回-1
public static void main(String[] args) {
String str = "hello world!";
int i1 = str.lastIndexOf("o",6);
System.out.println(i1); // 4 表示从下标6(w)开始从后往前搜索,只能搜索到第一个o
int i2 = str.indexOf("o",7);
System.out.println(i2); // 7 表示从下标7(第二个o)开始从后往前搜索,包含下标7(第二o)
}
(21)、replace(char oldChar, char newChar)
String replace(char oldChar, char newChar):返回一个新的字符串,它是通过用newChar替换此字符串中出现的所有oldChar得到的
public static void main(String[] args) {
String str = "hello world!";
String s1 = str.replace('o','s');
System.out.println(s1); // hells wsrld! 如果有多个,就替换多个 参数为字符类型
String s2 = str.replace('!','?');
System.out.println(s2); // hello world? 如果有一个就替换一个 参数为字符类型
}
(22)、replace(CharSequence target, CharSequence replacement)
String replace(CharSequence target, CharSequence replacement):区别于上面,参数可以为字符串类型
public static void main(String[] args) {
String str = "hello world!";
String s1 = str.replace("o","s");
System.out.println(s1); // hells wsrld! 如果有多个,就替换多个 参数可以为String类型
String s2 = str.replace("!","?");
System.out.println(s2); // hello world? 如果有一个就替换一个 参数可以为String类型
}
(23)、replaceAll(String regex, String replacement)
String replaceAll(String regex, String replacement):使用给定的replacement替换此字符串所有匹配给定的正则表达式的子字符串,以下演示没有使用正则表达式,用的是普通字符串进行分割(暂时不会正则,就不演示了),如果不使用正则的话,和上面
replace(CharSequence target Charsequence replacement)
方法一样,都是替换所有匹配的内容
public static void main(String[] args) {
String str = "hello world!";
String s1 = str.replaceAll("o","s");
System.out.println(s1); // hells wsrld! 如果有多个,就替换多个 第一个参数可以为正则表达式
String s2 = str.replaceAll("!","?");
System.out.println(s2); // hello world? 如果有一个就替换一个 第一个参数可以为正则表达式
}
(24)、replaceFirst(String regex, String replacement)
String replaceFirst(String regex, String replacement):使用给定的replacement替换此字符串匹配给定的正则表达式的第一个子字符串,以下演示没有使用正则表达式,用的是普通字符串进行分割(暂时不会正则,就不演示了)
public static void main(String[] args) {
String str = "hello world!";
String s1 = str.replaceFirst("o","s");
System.out.println(s1); // hells world! 只能替换第一个匹配的内容 第一个参数可以为正则表达式
}
(25)、split(String regex)
String[] split(String regex):根据给定正则表达式的匹配拆分此字符串,以下演示没有使用正则表达式,用的是普通字符串进行分割(暂时不会正则,就不演示了)
public static void main(String[] args) {
String str = "hello world 你好 世界";
String str2 = "hello,world,你好,世界";
String str3 = "hello我world我你好我世界";
String[] s = str.split(" ");
/*
hello
world
你好
世界
*/
Arrays.stream(s).forEach(System.out::println); // 使用stream流+lamdba遍历
/*
hello
world
你好
世界
*/
String[] s2 = str2.split(",");
Arrays.stream(s2).forEach(System.out::println);
/*
hello
world
你好
世界
*/
String[] s3 = str3.split("我");
Arrays.stream(s3).forEach(System.out::println);
}
(26)、split(String regex, int limit)
String[] split(String regex, int limit):根据匹配给定的正则表达式来拆分此字符串,最多不超过limit个,如果超过了,剩下的全部都放到最后一个元素中
public static void main(String[] args) {
String str = "hello world 你好 世界";
String str2 = "hello,world,你好,世界";
String str3 = "hello我world我你好我世界";
String[] s = str.split(" ",1);
/*
hello world 你好 世界
*/
Arrays.stream(s).forEach(System.out::println); // 使用stream流+lamdba遍历
System.out.println("******************");
/*
hello
world,你好,世界
*/
String[] s2 = str2.split(",",2);
Arrays.stream(s2).forEach(System.out::println);
System.out.println("******************");
/*
hello
world
你好我世界
*/
String[] s3 = str3.split("我",3);
Arrays.stream(s3).forEach(System.out::println);
}
(27)、matches(String regex)
boolean matches(String regex):告知此字符串是否匹配给定的正则表达式
public static void main(String[] args) {
String str = "897113177";
String str2 = "897113177@qq";
String str3 = "897113177@qq.com";
// 判断是否为邮箱的正则表达式
String regex = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
boolean b1 = str.matches(regex);
boolean b2 = str2.matches(regex);
boolean b3 = str3.matches(regex);
System.out.println(b1); // false
System.out.println(b2); // false
System.out.println(b3); // true
}
1.4、类型转换
String --> 基本数据类型/包装类:调用包装类的静态方法:parseXxx(String str)
基本数据类型/包装类 --> String:调用String重载的方法:valueOf(xxx)
1、String转基本数据类型、包装类
public static void main(String[] args) {
String str = "123";
String str2 = "true";
String str3 = "22.2";
// 转int
// int a = (int)str; 强转是错误的
int a = Integer.parseInt(str);
System.out.println(a); // 123
// 转布尔
boolean b = Boolean.parseBoolean(str2);
System.out.println(b); // true
// 转小数
double c = Double.parseDouble(str3);
System.out.println(c); // 22.2
}
2、基本数据类型、包装类转String
基本类型除了
short、byte
没有,其他的都有包装类型使用的参数为
Object obj
的重载方法
public static void main(String[] args) {
int i = 10;
boolean b = true;
Double d = 22.2;
String s1 = String.valueOf(i);
String s2 = String.valueOf(b);
String s3 = String.valueOf(d);
System.out.println(s1); // 10
System.out.println(s2); // true
System.out.println(s3); // 22.2
}
1.5、源码查看
从这里可以看出String以及存放所有字符的value都被声明为final类型的,是不可变的
这里是构造器的操作
1.6、小测验
不查看注释答案,判断结果是什么,并知道原因
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello" + "world";
String s5 = s1 + "world";
String s6 = "hello" + s2;
String s7 = s1 + s2;
final String s8 = "hello"; // 声明为常量,值就在常量池中
String s9 = s8 + "world"; // 相当于 "hello" + "world"
System.out.println(s3 == s9); // true
System.out.println(s3 == s4); // true
System.out.println(s3 == s5); // false
System.out.println(s3 == s6); // false
System.out.println(s3 == s7); // false
System.out.println(s5 == s6); // false
System.out.println(s5 == s7); // false
System.out.println(s6 == s7); // false
}
intern()
当调用intern方法时,如果池已经包含与
equals(Object)
方法确定的相当于此String
对象的字符串,则返回来自池的字符串。 否则,此String
对象将添加到池中,并返回对此String
对象的引用。
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
String s3 = "helloworld";
String s4 = "hello" + "world";
String s5 = s1 + "world";
String s6 = "hello" + s2;
String s7 = s1 + s2;
final String s8 = "hello"; // 声明为常量,值就在常量池中
String s9 = s8 + "world"; // 相当于 "hello" + "world"
System.out.println(s3 == s9); // true
// ****************使用intern()方法*************************
String s10 = (s1 + s2).intern();
System.out.println(s3 == s10); // true 如果常量池有,就用常量池的
String s11 = (s2 + s1).intern();
System.out.println(s3 == s11); // false 如果常量池中没有,就在常量池中添加值
System.out.println(s3 == s4); // true
System.out.println(s3 == s5); // false
System.out.println(s3 == s6); // false
System.out.println(s3 == s7); // false
System.out.println(s5 == s6); // false
System.out.println(s5 == s7); // false
System.out.println(s6 == s7); // false
}
结论
- 常量与常量的拼接结果在常量池。且常量池中不会存在相同内容的常量
- 只要其中有一个是变量,结果就在堆中
- 如果拼接的调用
intern()
方法,返回值就在常量池中
2、StringBuilder
特点:
- 是JDK5.0新增的类
- 区别于String,和StringBuffer是一类的,都是可变字符串
- 线程不安全的,但是性能好的
2.1、查看可变特点
下面在源码查看中可以了解到
2.2、构造器
public static void main(String[] args) {
// 相当于this.value = new char[16]
StringBuilder sb1 = new StringBuilder();
// this.value = new char[32]
StringBuilder sb2 = new StringBuilder(32);
// this.value = new char["abc".length] append("abc")
StringBuilder sb3 = new StringBuilder("abc");
}
2.3、常用方法
当append和insert时,如果原来value数组长度不够,可扩容
如果返回值为StringBuilder的,皆支持方法链操作,像下面这样
(1)、append(xxx)
StringBuffer append(xxx):提供了很多的append()重载方法,用于进行字符串拼接
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = sb1.append(123) // 没必要再用返回值接收了,返回值只是用来使用方法链操作的,原对象也会被修改
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
System.out.println(sb1); // 123true22.2hello,world
System.out.println(sb2); // 123true22.2hello,world
}
(2)、delete(int start,int end)
StringBuffer delete(int start,int end):删除指定位置的内容,左闭右开原则
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123)
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
// sb1:123true22.2hello,world
sb1.delete(0,3)
.delete(sb1.indexOf("w"),sb1.indexOf("d")+1) // 如果删除的是最后一个字符,需要下标+1,这么写要确保前面没有相同的字符
.delete(sb1.indexOf("t"),sb1.indexOf("2")) // 也可以选择要删除的最后一个字符的后一个字(在你知道后一个字的情况下,不建议),这么写要确保前面没有相同的字符,否则会异常,我这么写没错,因为我把前面的2删掉了
.delete(sb1.indexOf("2"),sb1.indexOf("2")); // 如果都指向同一个位置,则不删除
System.out.println(sb1); // 22.2hello,
}
(3)、replace(int start,int end,String str)
StringBuffer replace(int start,int end,String str):把[start,end)位置替换为str
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123)
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
// sb1:123true22.2hello,world
sb1.replace(0,3,"321")
.replace(sb1.indexOf("t"),sb1.indexOf("e")+1,"false");
System.out.println(sb1); // 321false22.2hello,world
}
(4)、insert(int offset,xxx)
StringBuffer insert(int offset,xxx):支持插入很多种类型的内容,在指定位置插入
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123)
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
// sb1:123true22.2hello,world
sb1.insert(3,321)
.insert(sb1.indexOf("d")+1,",你好,世界"); // 在最后一个字符的后面一位开始插入
System.out.println(sb1); // 123321true22.2hello,world,你好,世界
}
(5)、reverse()
StringBuffer reverse():把当前字符序列逆转
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123)
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
// sb1:123true22.2hello,world
sb1.reverse();
System.out.println(sb1); // dlrow,olleh2.22eurt321
}
(6)、indexOf(String str)
int indexOf(String str):获取对应字符串的下标,只获取第一次出现的字符,如果不存在,则返回-1
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123)
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
// sb1:123true22.2hello,world
int i1 = sb1.indexOf("2");
int i2 = sb1.indexOf("a");
System.out.println(i1); // 1
System.out.println(i2); // -1
}
(7)、subString(int start, int end)
String subString(int start, int end):截取指定下标内的字符,左闭右开原则
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123)
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
// sb1:123true22.2hello,world
String s1 = sb1.substring(3);
String s2 = sb1.substring(3, 7);
System.out.println(s1); // true22.2hello,world
System.out.println(s2); // true
}
(8)、length()
int length():获取字符串的长度
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123)
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
// sb1:123true22.2hello,world
int length = sb1.length();
System.out.println(length); // 22
}
(9)、charAt(int n)
char charAt(int n):获取指定下标上的字符
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123)
.append(true)
.append(22.2)
.append("hello")
.append(new StringBuilder(",world"));
// sb1:123true22.2hello,world
char c = sb1.charAt(2);
System.out.println(c); // 3
}
(10)、setCharAt(int n,char ch)
void setCharAt(int n,char ch):修改指定位置上的字符
public static void main(String[] args) {
StringBuilder sb1 = new StringBuilder();
sb1.append(123);
// sb1:123true22.2hello,world
sb1.setCharAt(0,'3');
System.out.println(sb1); // 323
}
2.4、源码查看
(1)、构造器
从构造器可以看出,默认容量是16,后面不够会进行扩容,可以自定义容量。
建议:如果容量大于16的话,使用自定义容量,减少扩容次数
(2)、append()方法
char[] value的默认容量是16,当添加的内容超出大小时,就会进行扩容机制
-
判断是否需要扩容
-
如果需要扩容,就判断新容量的大小
-
有两部分
- 第一部分是判断扩容(原来容量的两倍+2)后是否满足,如果不满足,容量就直接等于添加字符后的总容量。
- 第二部分判断扩容后的容量是否超过最大容量(2^31-1-8)后执行的操作
-
如果超出最大容量,就异常,否则如果比最大容量小,就用自己。如果相等,就用最大容量
3、StringBuffer
StringBuffer和StringBulider基本相同,皆是继承与
AbstractStringBuilder
类,这里就只讲StringBuilder类了,两者基本相同区别在于:
- StringBuffer在每个方法上都使用了
synchronized
关键字,是线程安全的,StringBuilder则是线程不安全的- StringBuffer因为添加了同步锁,所以性能没有StringBuilder好。
4、三者性能对比
通过下面对比,你会看到按速度排序:StringBuilder>StringBuffer>String
public static void main(String[] args) {
// 初始化
long startTime = 0L;
long endTime = 0L;
String s = "";
StringBuffer buffer = new StringBuffer();
StringBuilder builder = new StringBuilder();
// 开始对比 String
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
s += String.valueOf(i);
}
endTime = System.currentTimeMillis();
System.out.println("String执行时间:" + (endTime - startTime));
// StringBuffer
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
buffer.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuffer执行时间:" + (endTime - startTime));
// StringBuilder
startTime = System.currentTimeMillis();
for (int i = 0; i < 20000; i++) {
builder.append(String.valueOf(i));
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder执行时间:" + (endTime - startTime));
}
结果