ホームページ  >  記事  >  Java  >  Java での文字列の初期化

Java での文字列の初期化

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

文字列の初期化は、プログラミングの基本的な部分の 1 つです。文字列の初期化とは、Java プログラムで使用される前に変数に値を割り当てることを意味します。文字列の初期化は 2 つの方法で行うことができます:

  • オブジェクトの初期化
  • 直接初期化

文字列は Java のオブジェクトです。文字列とは、文字のシーケンスを指します。  文字の配列としても知られています。文字列は常に引用符で囲まれます。引用符であっても文字列の一部ではありません。文字列の初期化も 2 つのステップで行われ、1 つ目は宣言、2 つ目は初期化です。文字列宣言とは、明示的に値を割り当てずに文字列を宣言することを指します。ほとんどの使用例では、文字列の宣言のみが初期化を必要としません。

広告 このカテゴリーの人気コース JAVA マスタリー - スペシャライゼーション | 78 コース シリーズ | 15 回の模擬テスト

Java における文字列初期化の種類

Java では、文字列は複数の方法で初期化されます。コンストラクターを使用するか、リテラルを使用します。新しいキーワードを使用して String を初期化する場合と、リテラルを使用して String を初期化する場合には違いがあります。新しい Java オブジェクトを作成するたびに、新しいキーワードを使用して文字列を初期化します。定義されているが初期化されていない変数は、値を保持していないため、プログラムで使用できません。

1.オブジェクトの初期化

new キーワードを使用した文字列の初期化。 new キーワードを使用して String を初期化すると、メモリのヒープに新しいオブジェクトが作成されます。オブジェクトを通じて初期化された文字列は変更可能であるとは、初期化後に文字列の値を再割り当てできることを意味します。

String strMsg = new String("Be specific towards your goal!");

コンストラクターを使用してオブジェクトを開始すると、Java コンパイラーはヒープ メモリに新しいオブジェクトを作成します。ヒープ メモリは、JVM 用に予約されたメモリです。

2.直接初期化

リテラルを使用した文字列の初期化。リテラルを使用して文字列を初期化すると、メモリのプール領域にオブジェクトが作成されます。

String strMsg = "Be specific towards your goal!";

リテラルを使用して文字列を初期化するとき、それに割り当てられた値は別の文字列ですでに初期化されています。このようなシナリオでは、Literal を通じて作成された String は新しいオブジェクトを作成しません。以前に作成したオブジェクトへの参照を渡しただけです。

Java での文字列初期化の例

次に、Java での文字列初期化のさまざまな例を説明します。

例 #1

以下で作成された文字列では、どちらの文字列もリテラルを使用して作成されています。

コード:

String ObjStringFirst="Welcome";
String ObjStringSecond="Welcome";

上記の文字列では、文字列リテラルを使用してオブジェクトを作成しています。このような状況では、同じ値を持つオブジェクトがすでに存在している場合は、同じオブジェクトが返されます。それ以外の場合は、新しいオブジェクトが作成されます。

例 #2

以下の例では、文字列がどのように初期化されるかを確認できます。

コード:

public class StringExample {
public static void main(String[] args)
{
String nameFirst = "Alister";
String nameSecond = "Campbell";
String nameThird = new String("Adam");
String nameFourth = new String("Martin");
//String nameFifth;
String strNullFirst  = null;
String strNullSecond = null;
String strNullThird  = null;
System.out.println("String initialized :");
//printing variable which are initialized
System.out.println("First : " + nameFirst);
System.out.println("Second : " + nameSecond);
System.out.println("Third : " + nameThird);
//printing variable which are declared but not initialized
//System.out.println("String declared but not initialized :");
//System.out.println("Fifth : " + nameFifth);
}
}

出力:

Java での文字列の初期化

例 #3

この例では、String 変数の宣言が与えられています。宣言されているが、プログラムのコンパイル時に初期化されていない変数が含まれています。

コード:

public class StringExample {
public static void main(String[] args)
{
String nameFirst;
//printing variable which are declared but not initialized
System.out.println("String declared but not initialized :");
System.out.println("Fifth : " + nameFirst);
}
}

出力:

上記のプログラムの出力を以下に示します。上記のプログラムはコンパイル時にエラーを生成します。文字列は宣言されていますが、初期化されていません。

Java での文字列の初期化

String 宣言の後、宣言された String 変数に値が割り当てられた場合、コンパイルではエラーがスローされません (String の初期化には 2 つの部分が必要となるため)。 1 つ目は宣言、2 つ目は代入です。

例 #4

この例では、空の値を使用して文字列を初期化してみましょう。空の値で開始されたすべての String 変数は、3 つすべてに異なるオブジェクトを持ちます。これは、Literal で初期化された String は String プールにオブジェクトを作成し、new キーワードで初期化された String はメモリのヒープ領域にオブジェクトを作成するためです。

コード:

public class StringExample {
public static void main(String[] args)
{
String strVal1 = "";
String strVal2 = new String();
String strVal3 = new String("");
//printing variable which are initialized with empty values
System.out.println("String Values :");
System.out.println("First : " + strVal1);
System.out.println("Second : " + strVal2);
System.out.println("Third : " + strVal3);
}
}

出力:

上記のプログラムでは、文字列のオブジェクトも異なりますが、文字列に割り当てられる値は同じです。以下の出力は、各文字列変数に空の値がどのように割り当てられるかを示しています。

Java での文字列の初期化

例 #5

以下のプログラムでは、上記で初期化された文字列の比較が行われます。ここでは、文字列を比較する 3 つの方法を使用します。

1. == operator: In java == operator compare the references, not the values.
2. equals() method: It compares the variable’s value, not the references; hence, if the value matches both the String variable, it returns true other false.
3. compareTo() method: compareTo() method check the values & if values are identical then it return 0 otherwise it return + & – based on the below given comparison.

  • It returns 0 if str1 == str2
  • It returns positive value if str1 > str2
  • It returns negative value if str1 < str2

Code:

public class StringExample {
public static void main(String[] args)
{
String strVal1 = "";
String strVal2 = new String();
String strVal3 = new String("");
//printing variable which are initialized with empty values
System.out.println("String Values :");
System.out.println("First : " + strVal1);
System.out.println("Second : " + strVal2);
System.out.println("Third : " + strVal3);
System.out.println(" \nComparing strings using == operator :");
System.out.println("Comparing first two strings :");
System.out.println(strVal1 == strVal2);
System.out.println("Comparing last two strings :");
System.out.println(strVal2 == strVal3);
System.out.println("Comparing first & last strings :");
System.out.println(strVal1 == strVal3);
System.out.println(" \nComparing strings using equals method :");
System.out.println("Comparing first two strings :");
System.out.println(strVal1.equals(strVal2));
System.out.println("Comparing last two strings :");
System.out.println(strVal2.equals(strVal3));
System.out.println("Comparing first & last strings :");
System.out.println(strVal1.equals(strVal3));
System.out.println(" \nComparing strings using compareTo method :");
System.out.println("Comparing first two strings :");
System.out.println(strVal1.compareTo(strVal2));
System.out.println("Comparing last two strings :");
System.out.println(strVal2.compareTo(strVal3));
System.out.println("Comparing first & last strings :");
System.out.println(strVal1.compareTo(strVal3));
}
}

Output:

The output of the comparing string program is given below. It contains the output after comparing the Strings in three different ways; thus, outcomes of the comparison are also different according to the method applied.

Java での文字列の初期化

Conclusion

The above-given article explains the initialization of String & different types of ways to initialize the String in java. Also given examples in the article describe the internal creation of objects when initiating String using Literal. It also describes the difference between declaration & initialization.

以上がJava での文字列の初期化の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

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