首頁  >  文章  >  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
上一篇:設定超時Java下一篇:設定超時Java