ホームページ  >  記事  >  Java  >  Javaの文字列バッファ

Javaの文字列バッファ

WBOY
WBOYオリジナル
2024-08-30 16:01:56550ブラウズ

今日は Java の文字列バッファについて見ていきます。まず、Java について話しましょう。 Java はオブジェクト指向プログラミング言語です。これまでは、C、C++ 言語を使用してきました。これらの言語にはプラットフォームの問題がありました。 Java はプラットフォームに依存しない言語です。つまり、Java は Linux、MAC、Windows などのあらゆるオペレーティング システム上で実行できます。このトピックでは、Java の StringBuffer について学習します。

現在、世界中で 30 億台近くのデバイスが Java で動作しています。終わりのないテクノロジーのようです。機能の一部を以下に示します。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

  • Java には再利用可能なコードがたくさんあります。
  • 膨大なライブラリのセット
  • プラットフォームに依存しない
  • 自動ガベージコレクション。
  • セキュリティ
  • 大規模なコミュニティサポート

文字列 Buffer クラスを始める前に、Java がバックグラウンドでどのように動作するかを理解しておく必要があります。

Java は、私たちが知っているようにコンパイル言語です。正確にはどういう意味でしょうか?コンピューターがバイナリ形式の言語を認識していることは誰もが知っています。つまり、オーと 1 です。 Javaで書かれたプログラムを理解するにはコンパイラが必要です。 Java コンパイラは、次のコマンドを使用してこのプログラムをバイトコードに変換します。

Javac Someprogram.java

このコマンドは .class ファイルを作成します。このファイルはコンピュータ上で実行されています。

これで、Java プログラムで正確に何が起こるかが分かりました。

次に StringBuffer から始めましょう。

Java の StringBuffer とは何ですか?

Java では、文字列は一度作成すると変更できません。文字列を変更したり、新しい値を割り当てたりすることはできません。文字列オブジェクトを作成し、値を割り当てます。ここで、String Buffer クラスが登場します。文字列バッファーを使用すると、文字列を変更できます。

文字列とは何ですか?

それでは、文字列とは何なのかという明らかな疑問が頭に浮かびます。文字列は文字.

の組み合わせです。

次の例を見てください。

String city = new String("Mumbai");

次のように空の文字列を作成できます:

String city = new String();

文字列配列もあります。次の例を見てください:

String myArray1 = new String[3];

上記の例では、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 メソッドの使用 void ensureCapacity() キャラクターの制限を設定します。 str1.append(str2) str2 を文字列 str1 に追加します str1.setCharAt(n, ‘x’) n 番目の文字を x に変更します str1.setLength(n) str1 から n 文字までの長さ str1.insert(n, str2) これにより、str1 の n 番目の位置に str2 が挿入されます 内部容量() これにより、割り当てられた合計容量が返されます Int length() オブジェクトの現在の長さを返します テーブル>

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の文字列バッファの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。