Java中的字面量
字面量量:可以分配给变量的任何常量值都称为字面量量/常量。
简而言之, Java中的字面量是布尔、数字、字符或字符串数据的综合表示。它是在程序中表达特定值的一种媒介,比如一个名为''/count的整数变量在下面的语句中被赋值为一个整数值。
// Here 100 is a constant/literal.
int x = 100;
积分字面量
对于 Integral 数据类型(byte、short、int、long),我们可以通过 4 种方式指定字面量:-
十进制字面量(Base 10):在这种形式中,允许的数字是 0-9。
int x = 101;
八进制字面量(Base 8):在这种形式中,允许的数字是 0-7。
// The octal number should be prefix with 0.
int x = 0146;
Hexa-decimal 字面量 (Base 16):在这种形式中,允许的数字是 0-9,字符是 af。我们可以同时使用大写和小写字符,因为我们知道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);
}
}
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);
}
}
Java
// Java program to illustrate the
// application of char literals
public class Test {
public static void main(String[] args)
{
// single character literal 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");
}
}
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);
}
}
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);
}
}
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);
}
}
101
64
64206
15
Note : By default, every literal is of int type, we can specify explicitly as long type by suffixed with l or L. There is no way to specify byte and short literals explicitly but indirectly we can specify. Whenever we are assigning integral literal to the byte variable and if the value is within the range of byte then the compiler treats it automatically as byte literals.
浮点字面量量
对于浮点数据类型,我们只能以十进制形式指定字面量,而不能以八进制和十六进制形式指定。
十进制字面量(Base 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.230
123.222
Error: malformed floating point literal
Note: By default, every floating-point literal is of double type, and hence we cant assign directly to the float variable. But we can specify floating-point literal as float type by suffixed with f or F. We can specify explicitly floating-point literal as double type by suffixed with d or D. Of course this convention is not required.
字符字面量
对于 char 数据类型,我们可以通过 4 种方式指定字面量:
单引号:我们可以将字符数据类型的字面量指定为单引号内的单个字符。
char ch = 'a';
Char 字面量 as Integral 字面量:我们可以将 char 字面量指定为 integer 字面量,它表示字符的 Unicode 值,并且该整数字面量可以指定为十进制、八进制和十六进制形式。但允许的范围是 0 到 65535。
char ch = 062;
Unicode 表示:我们可以在 Unicode 表示 '\uxxxx' 中指定字符字面量。这里 xxxx 代表 4 个十六进制数。
char ch = '\u0061';// Here /u0061 represent a.
转义序列:每个转义字符都可以指定为字符字面量。
char ch = '\n';
例子:
Java
// Java program to illustrate the
// application of char literals
public class Test {
public static void main(String[] args)
{
// single character literal 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");
}
}
输出
a
error:Integer number too large
a
" is a symbol
字符串字面量
双引号内的任何字符序列都被视为字符串字面量。
String s = "Hello";
字符串字面量不得包含未转义的换行符或字符。但是, Java编译器将评估编译时表达式,因此以下 String 表达式会生成一个包含三行文本的字符串:
Example:
String text = "This is a String literal\n"
+ "which spans not one and not two\n"
+ "but three lines of text.\n";
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);
}
}
输出
Hello
error: cannot find symbol
symbol: variable Hello
location: class Test
布尔字面量
Boolean 字面量只允许使用两个值,即 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);
}
}
输出
true
false
error: incompatible types: int cannot be converted to boolean
error: incompatible types: int cannot be converted to boolean
Note: When we are performing concatenation operations, then the values in brackets are concatenated first. Then the values are concatenated from the left to the right. We should be careful when we are mixing character literals and integers in String concatenation operations and this type of operation are known as Mixed Mode operation.
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"