首页> Java> java教程> 正文

Java 文字

PHPz
发布: 2024-08-30 15:15:04
原创
162 人浏览过

文字是源中固定值的语法表示。在 Java 中,文字主要有四种类型:字符、布尔、数字和字符串。所以基本上,这些文字是一堆字符,用于存储任何变量的常量值。这些文字具有不可变的特征,我们无法更改它们。每当我们创建新变量或常量时,我们都会定义数据类型并分配特定值。现在,当编译器读取常量变量的值时,它会读取这些值并将其解释为文字。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

例如 const int abc = 123; abc 是我们的变量名,它是常量,并分配了值 123。现在常量变量的值 123 就是我们的文字。在 Java 中,我们有 5 种类型的重要文字。整数、布尔值、浮点值、字符和字符串用文字表示数据类型。当我们要将固定值传递到程序中时,我们会实现文字。

Java 文字类型及示例

我们现在将通过解释、演示程序和输出来学习上面提到的每个文字。

1.整数

八进制、二进制、十进制和十六进制数字是整数文字的文字值。允许使用 2、8、10 和 16 基数。

  • 二进制:0 和 1,
  • 八进制:基数 8
  • 十进制:基数 10 和
  • 十六进制:基数 16。

现在让我们用代码演示这些整数文字。

代码:

雷雷

代码说明:在我们的公共类中,我们有主类并声明了四个整数变量。我们的第一个 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 中呈现文字的方法,让我们演示并执行代码。

代码:

雷雷

代码说明:就像其他所有代码一样,我们有公共类和主类。然后我们将三个 char 变量分别声明为 ch1、ch2、ch3 和 value。后来三个打印语句。分配给这些变量的值不是普通的数字,而是代码,编译器会理解它们,并且输出将与值不同。

最后,我们有转义字符,它是单引号。请参阅下面所附的屏幕截图以获取完整的详细输出。

输出:

Java 文字

3.布尔文字

最简单的文字是布尔值,True 或 False。在这里,文字代表逻辑值:据我们所知,对于任何布尔数据类型只有两个。我们知道,在 Java 中,true 对应于值 1,而 false 代表值 0。让我们演示一个布尔值文字的工作示例。

代码:

雷雷

代码说明:我们有我们的类和主类,然后声明了两个布尔变量,并声明了各自的值。然后我们有打印语句,它将打印指定的布尔值。执行后,上面的代码将打印 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学习者快速成长!