Java StringBuilder类

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)
  1. StringBuilder (): creates an empty StringBuilder and reserves room for 16 characters.

    StringBuilder ():创建一个空的StringBuilder并保留16个字符的空间。

  2. StringBuilder (int size): create an empty string and takes an integer argument to set capacity of the buffer.

    StringBuilder (int size):创建一个空字符串,并使用一个整数参数来设置缓冲区的容量。

  3. StringBuilder (String str): create a StringBuilder object and initialize it with string str.

    StringBuilder (String str):创建一个StringBuilder对象,并使用字符串str对其进行初始化。

  4. 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 classStringBuilder class
StringBuffer is synchronized.StringBuilder is not synchronized.
Because of synchronisation, StringBuffer operation is slower than StringBuilder.StringBuilder operates faster.
StringBuffer is thread-safeStringBuilder is not thread-safe
StringBuffer is less efficient as compare to StringBuilderStringBuilder is more efficient as compared to StringBuffer.
Its storage area is in the heapIts storage area is the stack
It is mutableIt is mutable
Methods are synchronizedMethods are not synchronized
It is alternative of string classIt is more flexible as compared to the string class
Introduced in Java 1.0Introduced in Java 1.5
Its performance is moderateIts performance is very high
It consumes more memoryIt 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值