Javaリテラル

PHPz
リリース: 2024-08-30 15:15:04
オリジナル
230 人が閲覧しました

リテラルは、ソースからの固定値の構文表現です。 Java では、リテラルには主に文字、ブール、数値、文字列の 4 つのタイプがあります。基本的に、これらのリテラルは、変数の定数値を格納する文字の集まりです。これらのリテラルは不変の特性を持っており、変更することはできません。新しい変数または定数を作成するときは常に、データ型を定義し、特定の値を割り当てます。現在、コンパイラーは定数変数の値を読み取るときに、それらの値をリテラルとして読み取り、解釈します。

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

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

例: const int abc = 123; abc は変数名で、定数であり、値 123 が割り当てられています。ここで、定数変数のこの値 123 がリテラルです。 Java には 5 種類の重要なリテラルがあります。整数、ブール値、浮動小数点、文字、および文字列は、リテラルを使用してデータ型を表します。プログラムに固定値を渡す場合は、リテラルを実装します。

Java リテラルの種類と例

ここで、説明、デモンストレーション用のプログラム、および出力を使用して、上記のすべてのリテラルを学習します。

1.整数

8 進数、2 進数、10 進数、および 16 進数は、整数リテラルのリテ​​ラル値です。基数 2、8、10、および 16 の数値が使用できます。

  • バイナリ: 0 および 1、
  • 8 進数: 基数 8
  • 10 進数: 基数 10 および
  • 16 進数: 基数 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);
}
}
ログイン後にコピー

コードの説明: パブリック クラスでは、内部にメイン クラスがあり、4 つの整数変数が宣言されています。最初の int 変数は、値 1010 を持つ「m」です。これは、リテラル値の 10 進数形式です。次に、2 番目の変数「b」があり、リテラル値は 0110 (8 進数値) です。次に、16 進値 0x7e4 を持つ「o」変数、そして最後に、バイナリ形式のリテラル値 0b101 (「p」変数) です。実行すると、前述したように、さまざまな整変数のこれらの値がコンパイルされ、それぞれの形式として読み取られます。

上記のコードを実行すると整数値が返されます。適切な出力については、以下に添付されたスクリーンショットを参照してください。

出力:

Javaリテラル

2.文字リテラル

任意の 1 文字を一重引用符で囲むだけです。これはリテラル文字になりました。 char でリテラルを指定する方法は 4 つあります。

  • 単純な char リテラル: char ab = 'q';これは、文字データ型の単純なリテラルを指定します。
  • 整数リテラル: リテラル文字を指定する別の方法は、整数リテラルによるものです。 0 ~ 65535 の値を、10 進数、16 進数、または 8 進数の形式で、char リテラルとして指定できます。
  • Unicode: 文字リテラルは Unicode 形式、つまり「uxxx」で表すことができます。これら 4 つの x は 16 進数値です。
  • エスケープシーケンス: すべてのエスケープ文字は、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");
}
}
ログイン後にコピー

コードの説明: 他のコードと同じように、内部にはパブリック クラスとメイン クラスがあります。次に、3 つの char 変数をそれぞれ ch1、ch2、ch3、および value として宣言しました。後の 3 つの print ステートメント。これらの変数に割り当てられる値は単純な数値ではなく、コンパイラが理解できるコードであるため、出力は値とは異なります。

最後に、一重引用符であるエスケープ文字があります。詳細な出力については、以下に添付されたスクリーンショットを参照してください。

出力:

Javaリテラル

3.ブールリテラル

最も単純なリテラルは、True または False のブール値です。ここで、リテラルは論理値を表します。ご存知のとおり、どのブール データ型でも 2 つだけです。ご存知のとおり、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);
}
}
ログイン後にコピー

コードの説明: クラスとメイン クラスがあり、次にそれぞれの値で宣言された 2 つのブール変数があります。次に、割り当てられたブール値を出力する print ステートメントがあります。実行すると、上記のコードは 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 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!