首頁  >  文章  >  Java  >  Java中的字串初始化

Java中的字串初始化

WBOY
WBOY原創
2024-08-30 16:07:271043瀏覽

字串初始化是程式設計的基本部分之一。字串初始化意味著在java程式中使用變數之前為其賦值。字串初始化可以透過兩種方式完成:

  • 物件初始化
  • 直接初始化

字串是java中的一個物件。字串是指某些字元序列。  它也稱為字元數組。字串始終用引號表示;即使引號也不是字串的一部分。字串初始化也分兩步驟進行,第一步是聲明,第二步是初始化。字串宣告是指宣告一個字串而不明確地分配任何值。在大多數用例中,字串的唯一宣告不需要初始化。

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

Java 中字串初始化的型別

在java中,字串的初始化有多種方式。透過使用構造函數或使用文字。使用 new 關鍵字和使用 Literal 初始化 String 是有差異的。每次建立新的 java 物件時都使用 new 關鍵字初始化 String。已定義但未初始化的變數不能在程式中使用,因為它沒有保存任何值。

1.物件初始化

使用 new 關鍵字初始化字串。使用 new 關鍵字初始化 String 會在記憶體堆中建立一個新物件。透過物件初始化的字串是可變的,這意味著字串的值可以在初始化後重新分配。

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

當使用建構函式初始化物件時,java編譯器會在堆記憶體中建立一個新物件。堆記憶體是 JVM 保留的記憶體。

2.直接初始化

使用文字初始化字串。使用 Literal 初始化字串會在記憶體池區域中建立一個物件。

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

當使用文字初始化字串時,分配給它的值已經在另一個字串中初始化。在這種情況下,透過 Literal 建立的 String 不會建立新物件。它只是傳遞了對先前創建的物件的引用。

Java 中字串初始化的範例

以下解釋的是java中字串初始化的不同範例。

範例#1

在下面建立的字串中,兩個字串都是使用 Literal 建立的。

代碼:

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 初始化分為兩部分)。第一個是聲明,第二個是賦值。

範例#4

在此範例中,我們使用空值進行字串初始化。所有透過空值初始化的字串變數將具有不同的對象,這三個變數透過Literal 初始化的字串將在字串池中建立對象,而透過new 關鍵字初始化的字串將在記憶體堆區域創建對象。

代碼:

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);
}
}

輸出:

在上面給出的程式中,即使 Strings 的物件不同,但分配給 String 的值是相同的。下面給出的輸出顯示如何將空值指派給每個字串變數。

Java中的字串初始化

範例#5

在下面給出的程式中,給出了上面初始化字串的比較。這裡使用三種方式比較字串。

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中文網其他相關文章!

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