首頁 > Java > java教程 > 主體

Java 文字

PHPz
發布: 2024-08-30 15:15:04
原創
230 人瀏覽過

文字是來源中固定值的語法表示。在 Java 中,文字主要有四種類型:字元、布林、數字和字串。所以基本上,這些文字是一堆字符,用於儲存任何變數的常數值。這些文字具有不可變的特徵,我們無法更改它們。每當我們建立新變數或常數時,我們都會定義資料類型並指派特定值。現在,當編譯器讀取常數變數的值時,它會讀取這些值並將其解釋為文字。

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

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

例如 const int abc = 123; abc 是我們的變數名,它是常數,並分配了值 123。現在常數變數的值 123 就是我們的文字。在 Java 中,我們有 5 種類型的重要文字。整數、布林值、浮點值、字元和字串用文字表示資料類型。當我們要將固定值傳遞到程式中時,我們會實作文字。

Java 文字類型及範例

我們現在將透過解釋、演示程式和輸出來學習上面提到的每個文字。

1.整數

八進制、二進制、十進制和十六進制數字是整數文字的文字值。允許使用 2、8、10 和 16 基數。

  • 二進位: 0 和 1,
  • 八進位: 基數 8
  • 十進位: 基數 10 和
  • 十六進位: 基數 16。

現在讓我們用程式碼來示範這些整數文字。

代碼:

public class literal_b {
public static void main(String args[]) throws Exception{
int m = 1010;
int n = 0110;
int o = 0x7e4;
int p = 0b101;
System.out.println(m);
System.out.println(n);
System.out.println(o);
System.out.println(p);
}
}
登入後複製

程式碼說明:在我們的公共類別中,我們有主類別並聲明了四個整數變數。我們的第一個 int 變數是“m”,其值為 1010,它是文字值的十進位形式。然後我們有第二個變數“b”,其字面值為 0110,這是一個八進制值。接下來是我們的「o」變量,其十六進位值 0x7e4,最後是我們的二進位形式的文字值 0b101,它是我們的「p」變數。執行時,各種整數變數的這些值將被編譯並以各自的形式讀取,如上所述。

執行上述程式碼將傳回整數值;請參閱下方所附的螢幕截圖以取得正確的輸出。

輸出:

Java 文字

2.字符文字

只需用單引號將任何單一字元括起來;它現在是一個字面字元。有 4 種方法可以用 char 指定文字。

  • 簡單 char 文字: char ab = ‘q’;這指定了字元資料類型的簡單文字。
  • 整數文字:指定文字字元的另一種方法是透過整數文字。 0 到 65535 之間的值(以十進位、十六進位或八進位的形式)可以指定為 char 文字。
  • Unicode: 字元文字可以用 Unicode 形式表示,即「uxxx」;這四個x是十六進位值。
  • 轉義序列:每個轉義字元都可以作為 char 文字傳遞。

現在我們已經了解了上述在 char 中呈現文字的方法,讓我們示範並執行程式碼。

代碼:

public class literal_b{
public static void main(String args[]) throws Exception{
char ch1 = 'a';
char ch2 = 0123;
char ch3 = '\u0021';
System.out.println(ch1);
System.out.println(ch2);
System.out.println(ch3);
System.out.println("\'  is a escape character");
}
}
登入後複製

程式碼說明: 就像其他所有程式碼一樣,我們有公共類別和主類別。然後我們將三個 char 變數分別宣告為 ch1、ch2、ch3 和 value。後來三個印出語句。分配給這些變數的值不是普通的數字,而是程式碼,編譯器會理解它們,並且輸出將與值不同。

最後,我們有轉義字符,它是單引號。請參閱下面所附的螢幕截圖以獲取完整的詳細輸出。

輸出:

Java 文字

3.布林文字

最簡單的文字是布林值,True 或 False。在這裡,文字代表邏輯值:據我們所知,對於任何布林資料類型只有兩個。我們知道,在 Java 中,true 對應於值 1,而 false 代表值 0。讓我們示範一個布林值文字的工作範例。

代碼:

public class literal_b {
public static void main(String args[]) throws Exception{
boolean samlpeb1 = true;
boolean samlpeb2 = false;
System.out.println(samlpeb1);
System.out.println(samlpeb2);
}
}
登入後複製

程式碼說明:我們有我們的類別和主類,然後聲明了兩個布林變量,並聲明了各自的值。然後我們有列印語句,它將列印指定的布林值。執行後,上面的程式碼將列印 true 和 false。請參閱下面所附的螢幕截圖以了解預期輸出。

Output:

Java 文字

4. String

Anything that goes between double quotes, “literals,” is a string literal. Similar to char, except string literals can consist of multiple characters and are enclosed between double quotes.

The below code implements the string literal in the simplest way.

Code:

public class Main{
public static void main(String args[]) throws Exception{
String literalstr = "This is String Literal.";
System.out.println(literalstr);
}
}
登入後複製

Code Explanation: We have our simple class and the main class within, then we have our single most String Variable and the simple string value assigned to it. When printed, it will simply print the string as it is. Whatever is found between the double quotes will be recognized as string literal and will be reflected in the output as it is.

Refer to the below-attached screenshot for the output.

Output:

Java 文字

One of the critical aspects is understanding the difference between a variable, a constant, and a literal: Variables are used to store values for future use. Int a = 2 is an example of a data type with a variable name, and the value here is 2. Similar to variables, constants are used to store value, but values of constant variables cannot be changed once assigned; const int a = 3 is an example of a constant, and the value of a, which is 3, will never be changed by any method, in case if any attempt is made to change the value, the compiler won’t accept it. Now, literals are the values we assign to these variables and constants. Like earlier, a = 2 and a = 3, the values 2 and 3 are the literals.

Conclusion

Literals are the values we assign to variables, which can take various forms. Whatever the input form, the compiler will understand the code, and the output will be as expected. We understood literals for Boolean, Integer, and Character forms and implemented the understanding along with the code. These literals are best applicable when passing a fixed value in code.

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

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!