首頁  >  文章  >  Java  >  Java 中的 StringBuffer

Java 中的 StringBuffer

WBOY
WBOY原創
2024-08-30 16:01:56550瀏覽

今天我們將看到java中的String Buffer。首先,我們來談談java。 Java 是一種物件導向的程式語言。在此之前,我們一直在使用c、c++語言。對於這些語言,我們遇到了平台問題。 Java 是一種獨立於平台的語言。這意味著 java 可以在任何作業系統上運行,例如 Linux、MAC、Windows 等。在本主題中,我們將學習 Java 中的 StringBuffer。

當今世界上有近 30 億台設備在 Java 上運行。似乎是永無止境的技術。下面列出了一些功能。

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

  • Java 有很多可重複使用的程式碼。
  • 大量的庫
  • 平台獨立
  • 自動垃圾收集。
  • 安全
  • 強大的社群支持

在開始了解字串Buffer類別之前,我們必須了解java在幕後是如何運作的。

Java 擁有我們所知的編譯語言。到底是什麼意思?我們都知道計算機以二進制形式識別語言。即,o 和 1。我們需要一個編譯器來理解用java寫的程式。 Java 編譯器會使用以下命令將此程式轉換為字節碼。

Javac Someprogram.java

此指令建立 .class 檔案。此文件正在電腦上運行。

現在我們知道 java 程式到底發生了什麼事。

現在讓我們從 StringBuffer 開始。

Java中的StringBuffer是什麼?

在java中,字串一旦建立就無法變更。如果我們想改變字串或為字串分配新值是不可能的。它創建一個字串對象,然後分配值。這裡,String Buffer 類別就派上用場了。借助String Buffer,我們可以修改字串。

字串是什麼?

然後一個明顯的問題出現在你的腦海中,那麼字串是什麼?字串是字元.

的組合

看下面的例子。

String city = new String("Mumbai");

我們可以建立一個空字串,如下所示:

String city = new String();

我們還有字串陣列。看下面的範例:

String myArray1 = new String[3];

上面的範例建立了一個包含三個常數的字串陣列。

字串緩衝區在java中是如何運作的?

在 StringBuffer java 的幫助下,字串變得可變。

這種可變性到底代表什麼?看下面的例子。如果我們重新給java中的字串變數賦值,就會拋出錯誤,因為java中的字串是不可變的。

public class Str {
public static void main(String args[]){
//We are creating object here of StringBuffer class
StringBuffer city = new Stringbuffer("mumbai");
//let’s try to change the value of city here
City =" Delhi"
}
}

上面的程式碼將取得錯誤,因為字串是不可變的。

解釋 StringBuffer 類別

java.lang.StringBuffer 類別是一個線程安全類別。這意味著多個執行緒無法同時存取它們。它具有可變的字元序列。

StringBuffer類別與字串相同,但它是可變的,而字串是不可變的。每個 StringBuffer 的容量為 16 個字符,無需重新分配。它允許將項目添加到字串或字串開頭、中間或結尾處的子字串。 StringBuffer 是一個可增長的類別。

StringBuffer 方法依序運作。我們將看到這些方法以及它們是如何運作的。

StringBuffer 建構子:

StringBuffer r = new StringBuffer();

這將建立一個 StringBuffer 類別類型的空物件 r。

Java中StringBuffer類別的方法(含範例)

String Buffer 類別為我們提供了不同的方法來克服這個問題。

下面列出了一些方法。

StringBuffer Methods StvStringBuffer method use
void ensureCapacity() It sets the limit for the character.
str1.append(str2) It appends the str2 into string str1
str1.setCharAt(n, ‘x’) Modifies the nth character to x
str1.setLength(n) This length of str1 to the n characters
str1.insert(n, str2) This inserts str2 at the nth position in str1
Int capacity() This returns the total allocated capacity
Int length() It returns the current length of the object
StringBuffer 方法 StvStringBuffer 方法使用 無效的ensureCapacity() 它設定字元的限制。 str1.append(str2) 它將 str2 附加到字串 str1 str1.setCharAt(n, ‘x’) 將第 n 個字元修改為 x str1.setLength(n) str1 的長度到 n 個字元 str1.insert(n, str2) 這會將 str2 插入到 str1 中的第 n 個位置 整數容量() 這將傳回分配的總容量 整數長度() 它傳回物件的當前長度 表>

The above methods help the programmer to make the string mutable.

StringBuffer Methods in detail

Let’s look at the below program:

Save the below program with Example.java

class Example{
public static void main(String args[]){
StringBuffer abc = new StringBuffer("Hello World");
System.out.println("Welcome!!!"+ abc);
System.out.println("Total length of string is" + abc.length());
for(int i=0;i<=abc.length();i++){
int n = i+1;
System.out.println("We are using charAt function here :" + " " + n + " " + abc.charAt(i));
}
}
}

To run the above program, open the command prompt. Go to the location where the file is saved.

Then type the following two programs:

Javac Example.java

Java Example

ensureCapacity()

ensureCapacity() is the measure of the capacity to equal the specified minimumCapacity. That means the current capacity of StringBuffer is less than the argument of minimumCapacity so that the new internal array will get allocated with greater capacity.

class Ex2{
public static void main(String[] args)
{
StringBuffer abc = new StringBuffer();
// print string capacity
System.out.println("Before ensureCapacity the value is: "
+ "method capacity = "
+ abc.capacity());
abc.ensureCapacity(20);
System.out.println("After ensureCapacity the value is: + " method capacity = "
+ abc.capacity());
}
}

append() Method

append() method is used to append the value at the specified location.

The following example describes how we can append the string at the end of the existing string. This is one way to modify the string.

Example

class Ex2{
public static void main(String[] args)
{
StringBuffer abc = new StringBuffer("Welcome to the world ");
System.out.println("The string is:"  + abc );
abc.append("of java");
System.out.println("The string after appending is:"  + abc );
}
}

By appending a new string into the same variable, in the end, we modified the value of the string successfully.

In java, there are several predefined functions. We cannot write all the functions in a single article. But we can practice these functions one at a time. With the StringBuffer class, there are many methods to a string also.

Conclusion

StringBuffer class is used to make string mutable. This is an added advantage for us. If you are in the learning phase of java, you should learn how to play with strings. This is the initial level to start practicing programs written in java.

以上是Java 中的 StringBuffer的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn