搜尋
首頁Javajava教程Java中的字串初始化

字串初始化是程式設計的基本部分之一。字串初始化意味著在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

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
是否有任何威脅或增強Java平台獨立性的新興技術?是否有任何威脅或增強Java平台獨立性的新興技術?Apr 24, 2025 am 12:11 AM

新興技術對Java的平台獨立性既有威脅也有增強。 1)雲計算和容器化技術如Docker增強了Java的平台獨立性,但需要優化以適應不同雲環境。 2)WebAssembly通過GraalVM編譯Java代碼,擴展了其平台獨立性,但需與其他語言競爭性能。

JVM的實現是什麼,它們都提供了相同的平台獨立性?JVM的實現是什麼,它們都提供了相同的平台獨立性?Apr 24, 2025 am 12:10 AM

不同JVM實現都能提供平台獨立性,但表現略有不同。 1.OracleHotSpot和OpenJDKJVM在平台獨立性上表現相似,但OpenJDK可能需額外配置。 2.IBMJ9JVM在特定操作系統上表現優化。 3.GraalVM支持多語言,需額外配置。 4.AzulZingJVM需特定平台調整。

平台獨立性如何降低發展成本和時間?平台獨立性如何降低發展成本和時間?Apr 24, 2025 am 12:08 AM

平台獨立性通過在多種操作系統上運行同一套代碼,降低開發成本和縮短開發時間。具體表現為:1.減少開發時間,只需維護一套代碼;2.降低維護成本,統一測試流程;3.快速迭代和團隊協作,簡化部署過程。

Java的平台獨立性如何促進代碼重用?Java的平台獨立性如何促進代碼重用?Apr 24, 2025 am 12:05 AM

Java'splatformindependencefacilitatescodereusebyallowingbytecodetorunonanyplatformwithaJVM.1)Developerscanwritecodeonceforconsistentbehavioracrossplatforms.2)Maintenanceisreducedascodedoesn'tneedrewriting.3)Librariesandframeworkscanbesharedacrossproj

您如何在Java應用程序中對平台特定問題進行故障排除?您如何在Java應用程序中對平台特定問題進行故障排除?Apr 24, 2025 am 12:04 AM

要解決Java應用程序中的平台特定問題,可以採取以下步驟:1.使用Java的System類查看系統屬性以了解運行環境。 2.利用File類或java.nio.file包處理文件路徑。 3.根據操作系統條件加載本地庫。 4.使用VisualVM或JProfiler優化跨平台性能。 5.通過Docker容器化確保測試環境與生產環境一致。 6.利用GitHubActions在多個平台上進行自動化測試。這些方法有助於有效地解決Java應用程序中的平台特定問題。

JVM中的類加載程序子系統如何促進平台獨立性?JVM中的類加載程序子系統如何促進平台獨立性?Apr 23, 2025 am 12:14 AM

類加載器通過統一的類文件格式、動態加載、雙親委派模型和平台無關的字節碼,確保Java程序在不同平台上的一致性和兼容性,實現平台獨立性。

Java編譯器會產生特定於平台的代碼嗎?解釋。Java編譯器會產生特定於平台的代碼嗎?解釋。Apr 23, 2025 am 12:09 AM

Java編譯器生成的代碼是平台無關的,但最終執行的代碼是平台特定的。 1.Java源代碼編譯成平台無關的字節碼。 2.JVM將字節碼轉換為特定平台的機器碼,確保跨平台運行但性能可能不同。

JVM如何處理不同操作系統的多線程?JVM如何處理不同操作系統的多線程?Apr 23, 2025 am 12:07 AM

多線程在現代編程中重要,因為它能提高程序的響應性和資源利用率,並處理複雜的並發任務。 JVM通過線程映射、調度機制和同步鎖機制,在不同操作系統上確保多線程的一致性和高效性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具