StringBuilder is identical to StringBuffer except for one important difference that it is not synchronized, which means it is not thread safe.
StringBuilder与StringBuffer相同,除了一个重要的区别是它不同步,这意味着它不是线程安全的。
StringBuilder also used for creating string object that is mutable and non synchronized. The StringBuilder class provides no guarantee of synchronization. StringBuffer and StringBuilder both are mutable but if synchronization is not required then it is recommend to use StringBuilder class.
StringBuilder还用于创建可变且不同步的字符串对象。 StringBuilder类不能保证同步。 StringBuffer和StringBuilder都是可变的,但是如果不需要同步,则建议使用StringBuilder类。
This class is located into java.lang
package and signature of the class is as:
该类位于java.lang
包中,该类的签名为:
public final class StringBuilder
extends Object
implements Serializable, CharSequence
StringBuilder构造函数 (StringBuilder Constructors)
StringBuilder (): creates an empty StringBuilder and reserves room for 16 characters.
StringBuilder ():创建一个空的StringBuilder并保留16个字符的空间。
StringBuilder (int size): create an empty string and takes an integer argument to set capacity of the buffer.
StringBuilder (int size):创建一个空字符串,并使用一个整数参数来设置缓冲区的容量。
StringBuilder (String str): create a StringBuilder object and initialize it with string str.
StringBuilder (String str):创建一个StringBuilder对象,并使用字符串str对其进行初始化。
StringBuilder (CharSequence seq): It creates stringbuilder object by using CharSequence object.
StringBuilder (CharSequence seq):它使用CharSequence对象创建stringbuilder对象。
创建一个StringBuilder类 ( Creating a StringBuilder Class )
Lets use StringBuilder class to create string object and check its mutability also.
让我们使用StringBuilder类创建字符串对象并检查其可变性。
public class Demo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("study");
System.out.println(sb);
// modifying object
sb.append("tonight.com");
System.out.println(sb);
}
}
study studytonight.com
学习studytonight.com
StringBuffer和StringBuilder类之间的区别 (Difference between StringBuffer and StringBuilder class)
StringBuffer class | StringBuilder class |
---|---|
StringBuffer is synchronized. | StringBuilder is not synchronized. |
Because of synchronisation, StringBuffer operation is slower than StringBuilder. | StringBuilder operates faster. |
StringBuffer is thread-safe | StringBuilder is not thread-safe |
StringBuffer is less efficient as compare to StringBuilder | StringBuilder is more efficient as compared to StringBuffer. |
Its storage area is in the heap | Its storage area is the stack |
It is mutable | It is mutable |
Methods are synchronized | Methods are not synchronized |
It is alternative of string class | It is more flexible as compared to the string class |
Introduced in Java 1.0 | Introduced in Java 1.5 |
Its performance is moderate | Its performance is very high |
It consumes more memory | It consumes less memory |
StringBuffer类 | StringBuilder类 |
---|---|
StringBuffer已同步。 | StringBuilder不同步。 |
由于同步,StringBuffer操作比StringBuilder慢。 | StringBuilder的运行速度更快。 |
StringBuffer是线程安全的 | StringBuilder不是线程安全的 |
与StringBuilder相比,StringBuffer的效率较低 | 与StringBuffer相比,StringBuilder效率更高。 |
它的存储区在堆中 | 它的存储区域是堆栈 |
易变 | 易变 |
方法是同步的 | 方法不同步 |
字符串类的替代 | 与字符串类相比,它更加灵活 |
在Java 1.0中引入 | 在Java 1.5中引入 |
性能中等 | 它的性能很高 |
它消耗更多的内存 | 它消耗更少的内存 |
StringBuffer类的示例 (Example of StringBuffer class)
public class BufferDemo{
public static void main(String[] args){
StringBufferobj=new StringBuffer("Welcome to ");
obj.append("studytonight.com");
System.out.println(obj);
}
}
Welcome to studytonight.com
欢迎来到studytonight.com
Stringbuilder类的示例 (Example of Stringbuilder Class)
public class BuilderDemo{
public static void main(String[] args){
StringBuilderobj=new StringBuilder("Welcome to ");
obj.append("studytonight.com");
System.out.println(obj);
}
}
Welcome to studytonight.com
欢迎来到studytonight.com
StringBuilder方法 (StringBuilder Methods )
StringBuilder class has various methods to handle string object, such as append, insert, replace, reverse etc. Lets see usage of these with the help of examples.
StringBuilder类具有多种处理字符串对象的方法,例如追加,插入,替换,反向等。让我们在示例的帮助下查看它们的用法。
StringBuilder附加字符串示例 (Example of StringBuilder append string)
In this example, we are appending a new string using appen() method to the existing string object.
在此示例中,我们使用appen()方法将新字符串附加到现有字符串对象上。
public class Demo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("study");
System.out.println(sb);
// appending object
sb.append("tonight.com");
System.out.println(sb);
}
}
study studytonight.com
学习studytonight.com
StringBuilder替换方法 (StringBuilder Replace Method)
It is used to replace a substring from the string object. This method takes three arguments, first is start index, second is last index and third is substring to be replaced.
它用于替换字符串对象中的子字符串。 此方法采用三个参数,第一个是开始索引,第二个是最后一个索引,第三个是要替换的子字符串。
public class Demo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java is a programming language");
System.out.println(sb);
// replacing object
sb.replace( 10, 21, "computer");
System.out.println(sb);
}
}
Java is a programming language Java is a computer language
Java是一种编程语言Java是一种计算机语言
StringBuilder反向方法 (StringBuilder Reverse Method)
It is used to reverse the string object. It completely reverses the string from start to end characters. See the below example.
它用于反转字符串对象。 它将字符串从头到尾完全颠倒。 请参见以下示例。
public class Demo {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Java stringbuilder");
System.out.println(sb);
// reverse object
sb.reverse();
System.out.println(sb);
}
}
Java stringbuilder redliubgnirts avaJ
Java stringbuilder redliubgnirts avaJ
翻译自: https://www.studytonight.com/java/stringbuilder-class.php