Java中的字符串是由字符数组在内部支持的对象。由于数组是不可变的(不能增长),字符串也是不可变的。每当对 String 进行更改时,都会创建一个全新的 String。但是, Java提供了多个类,通过这些类可以使用字符串。两个这样的类是 StringBuffer 和 StringBuilder。在本文中,我们将看到这两个类之间的区别。
StringBuffer 类: StringBuffer 是 String 的对等类,它提供了字符串 的大部分功能。字符串表示固定长度的、不可变的字符序列,而 StringBuffer 表示可增长和可写的字符序列。 StringBuffer 可能会在中间插入字符和子字符串或附加到末尾。它会自动增长,为此类添加腾出空间,并且通常会预先分配比实际需要更多的字符,以留出增长空间。为了创建一个字符串缓冲区,需要创建一个对象,(即),如果我们希望创建一个名为 str 的新字符串缓冲区,那么:
StringBuffer str = new StringBuffer();
示例:以下是实现 StringBuffer 类的示例。
// Java program to demonstrate
// the StringBuffer class
public class GFG {
// Driver code
public static void main(String args[])
{
// Creating a new StringBuffer
StringBuffer str
= new StringBuffer("Hello");
str.append(" World!");
System.out.println(str);
}
}
Hello World!
StringBuilder 类:与 StringBuffer 类似, Java的 StringBuilder 表示可变的字符序列。由于Java的 String Class 创建了一个不可变的字符序列,StringBuilder 类提供了 String Class 的替代方案,因为它创建了一个可变的字符序列。 StringBuilder 的函数与 StringBuffer 类非常相似,因为它们都通过创建可变的字符序列来替代 String 类。与 StringBuffer 类似,为了创建一个名为 str 的新字符串,我们需要创建一个 StringBuilder 对象,(即):
StringBuilder str = new StringBuilder();
示例:以下是实现 StringBuilder 类的示例。
// Java program to demonstrate
// the StringBuilder class
public class GFG {
// Driver code
public static void main(String args[])
{
// Creating a new StringBuilder
StringBuilder str
= new StringBuilder("Hello");
str.append(" World!");
System.out.println(str);
}
}
Hello World!
从 StringBuffer 到 StringBuilder 的转换
StringBuffer 不能直接转换为 StringBuilder。我们首先需要使用内置方法toString()将 StringBuffer 转换为 String 对象。将其转换为字符串对象后,我们可以使用类的构造函数简单地创建一个 StringBuilder。例如:
// Java program to demonstrate
// the conversion between the
// StringBuffer and StringBuilder
// class
public class GFG {
// Driver code
public static void main(String args[])
{
StringBuffer sbr
= new StringBuffer("Geeks");
// Conversion from StringBuffer
// object to the String object
String str = sbr.toString();
// Creating a new StringBuilder
// using the constructor
StringBuilder sbl
= new StringBuilder(str);
System.out.println(sbl);
}
}
Geeks
从 StringBuilder 到 StringBuffer 的转换
与上面的转换类似,StringBuilder 不能直接转换为StringBuffer。我们首先需要使用内置方法toString()将 StringBuilder 转换为 String 对象。现在,我们可以使用构造函数创建一个 StringBuilder。例如:
// Java program to demonstrate
// the conversion between the
// StringBuilder and StringBuffer
// class
public class GFG {
// Driver code
public static void main(String args[])
{
StringBuilder sbr
= new StringBuilder("Geeks");
// Conversion from StringBuilder
// object to the String object
String str = sbr.toString();
// Creating a new StringBuffer
// using the constructor
StringBuffer sbl
= new StringBuffer(str);
System.out.println(sbl);
}
}
Geeks
Java的StringBuilder 与 StringBuffer
StringBuffer Class | StringBuilder Class |
---|---|
StringBuffer is present in Java. | StringBuilder was introduced in Java 5. |
StringBuffer is synchronized. This means that multiple threads cannot call the methods of StringBuffer simultaneously. | StringBuilder is asynchronized. This means that multiple threads can call the methods of StringBuilder simultaneously. |
Due to synchronization, StringBuffer is called a thread safe class. | Due to its asynchronous nature, StringBuilder is not a thread safe class. |
Due to synchronization, StringBuffer is lot slower than StringBuilder. | Since there is no preliminary check for multiple threads, StringBuilder is a lot faster than StringBuffer. |