首页  >  文章  >  Java  >  Java中的StringBuffer类

Java中的StringBuffer类

WBOY
WBOY原创
2024-08-30 15:42:11550浏览

众所周知,java 对象基本上有两种类型,它们是可变的和不可变的。不可变对象是指一旦创建其内容就无法修改的对象。每当不可变对象的内容发生更改时,都会创建新对象。对于可变对象,我们可以更改现有对象的内容,但这不会导致创建新对象。因此,可变字符串是那些可以在不创建新对象的情况下更改其内容的字符串。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

StringBuffer和StringBuilder是java中String的可变版本,而java String类是不可变的。因此 StringBuffer 是一个可变字符串,用于为字符串对象提供可变性。 String 类包含固定长度、不可变的字符序列,而字符串缓冲区具有可扩展的长度和可写的字符序列。

如何在Java中使用字符串缓冲区类?

这里有一些要点展示了我们如何在 java 中使用 StringBuffer。

  • 如前所述,java 中的可变字符串可以使用 StringBuffer 和 StringBuilder 类创建。
  • 两者之间的主要区别在于 StringBuffer 是线程安全的实现,而 StringBuilder 则不是。
  • 每当需要高性能和高安全性时,人们应该更喜欢 String 类的可变版本。
  • 由于String池的存在,String类存在安全问题;因此,每当需要数据安全时,就会使用 StringBuffer。
  • StringBuffer 在性能方面比 StringBuffer 更好,因为 StringBuffer 是线程安全的,但每当需要线程安全时,就应该选择 StringBuffer。

StringBuffer 构造函数

以下是字符串缓冲区构造函数:

  • StringBuffer(): 这会创建一个空的 StringBuffer,默认容量为 16 个字符。
  • StringBuffer(intcapacity): 这将创建一个具有指定容量的空 StringBuffer。
  • StringBuffer(CharSequence charseq): 这将创建包含与指定字符序列中相同字符的 StringBuffer。
  • StringBuffer(String str): 创建与指定 String 对应的 StringBuffer。

这是 StringBuffer 类的声明:

public final class StringBuffer extends Object implements Serializable,CharacterSequence,Appendable

Java中StringBuffer类的方法

现在我们将了解 StringBuffer 中可用的不同方法和字段。以下是 StringBuffer 类中常用方法的列表:

方法名称 描述
长度()和容量() 可变字符串的长度可以使用length()方法计算,相应的容量可以使用capacity()计算。
追加(字符串str)

追加(整数)

此方法用于在现有字符串缓冲区的末尾添加新文本。
插入(int索引,字符串str)

插入(int索引,char ch)

用于在给定字符串的指定位置插入文本。在给定的语法中,索引指定要插入字符串的起始索引。
反向() 用于反转给定字符串缓冲区对象中的字符顺序。
删除(int start, int end)和deleteCharAt(int index) 用于从字符串缓冲区中删除字符。 Start 表示要删除的第一个字符的起始索引,end 表示要删除的最后一个字符的索引。
替换(int startindex, int endindex, String str) 用于用指定的字符串缓冲区替换startindex和endindex-1之间的字符序列。
charAt(int 索引) 用于返回字符串缓冲区中指定索引处的字符。
codePointAt(int 索引) 用于返回指定索引处字符的 Unicode。
codePointBefore(int 索引) 用于返回指定索引之前的字符的Unicode。
子字符串(int start)

substring(int start, int end)

Used to return a new String that contains a subsequence of characters contained in a given string.
ensureCapacity(int capacity) Used for increasing the capacity of an existing string buffer object.
toString() Used to convert mutable string buffer to an immutable string object.

Examples of StringBuffer Class in Java

Here are some of the examples of StringBuffer class which are given below:

Example #1

Let us see a basic example of the StringBuffer class.

Code:

public class StringBufferDemo{
public static void main(String args[]){
StringBuffer sBuffer1=new StringBuffer("Welcome");
System.out.println("Original String is ::: " + sBuffer1 + ":: having length " + sBuffer1.length());
//using append method
sBuffer1.append(" To Edubca");
System.out.println("Modified String after append is :: " + sBuffer1 + " :: having length " + sBuffer1.length());
//using reverse method
sBuffer1.reverse();
System.out.println("Modified String after Reverse is :: " + sBuffer1);
}
}

The above code shows the creation of java StringBuffer and its different methods. The following output will be produced.

Output:

Java中的StringBuffer类

Example #2

In this example, we will see some more methods of the StringBuffer class.

Code:

public class StringBufferDemo{
public static void main(String args[]){
StringBuffer sBuffer=new StringBuffer ("WelcomeToEdubca");
System.out.println("Original String is ::: " + sBuffer + ":: having length " + sBuffer.length());
//using replace method
sBuffer.replace(0,9,"This is ");
System.out.println("Modified String after replace is :: " + sBuffer + " :: having length " + sBuffer.length());
//using delete method
sBuffer.delete(0,7);
System.out.println("Modified String after delete is :: " + sBuffer);
}
}

The above code will display the following as output.

 Output:

Java中的StringBuffer类

In the above example, we have seen how to create a StringBuffer class and usage of its methods.

Conclusion

From the above discussion, we have a clear understanding of StringBuffer in java, how it is created, and the different methods available in the StringBuffer class. Also, StringBuffer is thread-safe; therefore, it can be used in a multithreading environment.

以上是Java中的StringBuffer类的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:settimeout Java下一篇:Java ArrayList Class