字面意义的: 任何可以分配给变量的常量值都称为literal/constant。
简单地说,Java中的文字是布尔、数字、字符或字符串数据的综合表示。它是一种在程序中表达特定值的媒介,例如,在下面的语句中,名为“”/count的整数变量被分配了一个整数值。
// Here 100 is a constant/literal.int x = 100;
整型文字
对于整型数据类型(byte、short、int、long),我们可以用4种方式指定文字:-
十进制文字(以10为基数): 在此表格中,允许的数字为0-9。
int x = 101;
八进制文字(以8为基数): 在此表格中,允许的数字为0-7。
// The octal number should be prefix with 0.int x = 0146;
十六进制文字(以16为基数): 在这种形式中,允许的数字是0-9,字符是a-f。我们可以使用大写和小写字符,因为我们知道java是区分大小写的编程语言,但这里java不区分大小写。
// The hexa-decimal number should be prefix// with 0X or 0x.int x = 0X123Face;
二进制文字: 从1.7开始,我们甚至可以以二进制形式指定文本值,允许的数字是0和1。文本值的前缀应为0b或0b。
int x = 0b1111;
例子:
JAVA
// Java program to illustrate the // application of Integer literals public class Test { public static void main(String[] args) { // decimal-form literal int a = 101 ; // octal-form literal int b = 0100 ; // Hexa-decimal form literal int c = 0xFace ; // Binary literal int d = 0b1111; System.out.println(a); System.out.println(b); System.out.println(c); System.out.println(d); } } |
101646420615
注: 默认情况下,每个文本都是int类型,我们可以通过后缀为l或l显式指定为long类型。无法显式指定字节和短文本,但可以间接指定。每当我们为字节变量赋值整型文字时,如果值在字节范围内,编译器就会自动将其视为字节文字。
浮点文字
对于浮点数据类型,我们只能以十进制形式指定文本,不能以八进制和十六进制形式指定。
十进制文字(以10为基数): 在此表格中,允许的数字为0-9。
double d = 123.456;
JAVA
// Java program to illustrate the // application of floating-point literals public class Test { public static void main(String[] args) { // decimal-form literal float a = 101.230 ; // It also acts as decimal literal float b = 0123.222 ; // Hexa-decimal form (error) float c = 0x123 . 222 ; System.out.println(a); System.out.println(b); System.out.println(c); } } |
输出
101.230123.222Error: malformed floating point literal
注: 默认情况下,每个浮点文本都是双精度类型,因此我们不能直接分配给浮点变量。但我们可以通过后缀为f或f将浮点文本指定为浮点类型。我们可以通过后缀为d或d将浮点文本显式指定为双类型。当然,这个约定不是必需的。
字符文字
对于字符数据类型,我们可以通过4种方式指定文字:
单引号: 我们可以将字符数据类型的文本指定为单引号中的单个字符。
char ch = 'a';
字符文字作为整型文字: 我们可以将char literal指定为integral literal,它表示字符的Unicode值,该integral literal可以以十进制、八进制和十六进制形式指定。但允许的范围是0到65535。
char ch = 062;
Unicode表示法: 我们可以在Unicode表示形式“uxxx”中指定字符文本。这里xxxx代表4个十六进制数。
char ch = 'u0061';// Here /u0061 represent a.
转义序列: 每个转义字符都可以指定为字符文本。
char ch = '';
例子:
JAVA
// Java program to illustrate the // application of char literals public class Test { public static void main(String[] args) { // single character literl within single quote char ch = 'a' ; // It is an Integer literal with octal form char b = 0789 ; // Unicode representation char c = 'u0061' ; System.out.println(ch); System.out.println(b); System.out.println(c); // Escape character literal System.out.println( "" is a symbol" ); } } |
输出
aerror:Integer number too largea" is a symbol
字符串常量
双引号内的任何字符序列都被视为字符串文字。
String s = "Hello";
字符串文字不能包含未转换的换行符或换行符。但是,Java编译器将计算编译时表达式,因此以下字符串表达式将生成一个包含三行文本的字符串:
Example:String text = "This is a String literal" + "which spans not one and not two" + "but three lines of text.";
JAVA
// Java program to illustrate the // application of String literals public class Test { public static void main(String[] args) { String s = "Hello" ; // If we assign without "" then it treats // as a variable and causes compiler error String s1 = Hello; System.out.println(s); System.out.println(s1); } } |
输出
Helloerror: cannot find symbolsymbol: variable Hellolocation: class Test
布尔字面常数
布尔文字只允许有两个值,即true和false。
boolean b = true;
JAVA
// Java program to illustrate the // application of boolean literals public class Test { public static void main(String[] args) { boolean b = true ; boolean c = false ; boolean d = 0 ; boolean b = 1 ; System.out.println(b); System.out.println(c); System.out.println(d); System.out.println(e); } } |
输出
truefalseerror: incompatible types: int cannot be converted to booleanerror: incompatible types: int cannot be converted to boolean
注: 当我们执行连接操作时,首先连接括号中的值。然后,从左到右连接这些值。当我们在字符串连接操作中混合字符文本和整数时,我们应该小心,这种类型的操作称为 混合模式运行 .
JAVA
// Java program to illustrate the behaviour of // char literals and integer literals when // we are performing addition public class Test { public static void main(String[] args) { // ASCII value of 0 is 48 int first = '0' ; // ASCII value of 7 is 55 int second = '7' ; System.out.println( "Geeks!" + first + '2' + second); } } |
Geeks!48255
说明: 每当我们在字符串和整数之间执行加法时,整个结果都会转换为字符串。上述项目评估按以下方式进行:
"Geeks!" + first + '2' + second"Geeks! " + 48 + '2' + 55"Geeks!48" + '2' + 55"Geeks!482" + 55"Geeks!48255"
本文由 比沙尔·库马尔·杜比 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论。