1. Overview
1.概述
While working with the Java programming language, we’ll use plenty of literals.
在使用Java编程语言的过程中,我们会使用大量的字面符号。
In this tutorial, we’ll look at all the types of literals and how to use them.
在本教程中,我们将研究所有类型的字元以及如何使用它们。
2. What Is a Java Literal?
2.什么是Java字面意思?
A Java literal is any value we specify as a constant in the code. It can be of any type – integer, float, double, long, String, char, or boolean.
Java literal是我们在代码中指定为常量的任何值。它可以是任何类型–整数,浮点数,双数,长数,字符串,字符,或布尔值。
In the example below, the number 1 and the string literal_string are the literals.
在下面的例子中,数字1和字符串literal_string是字面意思。
int x = 1;
String s = "literal_string";
When working with literals, it’s equally important to have a good understanding of Java primitives.
在处理字词时,对Java基元有良好的理解同样重要。
3. Types of Literals
3.字母的类型
Let’s look at the different types of literals with some examples.
让我们通过一些例子来看看不同类型的字元。
3.1. Integer Literals
3.1 整数字元
For integral numbers (int, long, short, byte), we can specify them in different ways.
对于整数(int, long, short, byte),我们可以用不同的方式来指定。
Firstly, we can use decimal literals (base 10). These are the most used as they are the numbers we use daily.
首先,我们可以使用十进制字元(base 10)。这些是最常用的,因为它们是我们日常使用的数字。
int x = 1;
Secondly, we can specify integer literals in octal form (base 8). In this form, they’ve to start with a 0.
其次,我们可以用八进制形式(base 8)指定整数字。在这种形式下,它们必须以0.开头。
int x = 05;
Thirdly, integer literals can be used in hexadecimal form (base 16). They’ve to start with a 0x or 0X.
第三,整数字元可以用十六进制形式(base 16)。它们必须以0x或0X开始。
int x = 0x12ef;
Lastly, we have the binary form (base 2). This form was introduced in Java 1.7, and these literals have to start with 0b or 0B.
最后,我们有二进制形式(base 2)。这种形式是在Java 1.7中引入的,这些字词必须以0b 或0B开始。
int x = 0b1101;
In the examples above, we can change int to any integer type as long as the literal value doesn’t exceed the type limit.
在上面的例子中,我们可以将int改为任何整数类型,只要字面价值不超过类型限制。
These literals are considered int by default. For long, short, or byte, the compiler checks if the value is up against the limits of the type, and if true, it considers them as that type literal.
默认情况下,这些字词被视为int。对于 long、 short或byte,编译器会检查该值是否达到了该类型的限制,如果为真,则将其视为该类型字面。
We can override the default int literal by using l or L for long literals. We need to use this only when the literal value is above the int limit.
我们可以通过使用l或L来覆盖默认的int字头的长文字。只有当字面价值高于int限制时,我们才需要使用这个。
3.2. Floating-Point Literals
3.2.Floating-Point字样
Floating-point literals are considered double by default. We can also specify that the literal’s a double by using d or D. It’s not mandatory, but it’s a good practice.
浮点字头默认为double。我们也可以通过使用d或D来指定字头是double。这不是强制性的,但这是一个好的做法。
We can specify that we want a float by using f or F. This is mandatory for literals of type float.
我们可以通过使用f或F来指定我们想要一个float。这对于float类型的字词来说是强制性的。
Floating-point literals can be only specified in decimal form (base 10):
浮点字词只能以十进制形式(基数10)指定。
double d = 123.456;
float f = 123.456;
float f2 = 123.456d;
float f3 = 123.456f;
The second and third examples won’t compile due to the type mismatch.
由于类型不匹配,第二个和第三个例子不会被编译。
3.3. Char Literals
3.3.字母
For char data type, literals can come in a few ways.
对于char数据类型,字面意义可以有几种方式。
Single-quote characters are common and are especially useful for special characters.
单引号字符很常见,对特殊字符尤其有用。
char c = 'a';
char c2 = '\n';
We can use only one single character or a special character between the single-quotes.
我们可以在单引号之间只使用一个单字符或一个特殊字符。
The integer values used for a character are translated to the Unicode value of that character:
用于一个字符的整数值被翻译成该字符的Unicode值。
char c = 65;
We can specify it in the same way as the integer literals.
我们可以用与整数字面相同的方式指定它。
Lastly, we can use the Unicode representation:
最后,我们可以使用Unicode表示法。
char c= '\u0041';
The expressions in the last two examples evaluate to character A.
最后两个例子中的表达式评估为字符A.。
3.4. String Literals
3.4.字符串字元
Any text between double quotes is a String literal:
双引号之间的任何文本都是一个String字面。
String s = "string_literal";
String literals can only be on one line. In order to have a multi-line String, we can use an expression that will be executed at compile time:
String字元只能在一行。 为了得到一个多行的String,我们可以使用一个表达式,在编译时执行。
String multiLineText = "When we want some text that is on more than one line,\n"
+ "then we can use expressions to add text to a new line.\n";
4. Short and Byte Literals
4.短码和字节字元
Above, we saw how to declare a literal of every type. For all the types, except byte and short, we don’t need to create a variable. We can simply use the literal value.
上面,我们看到了如何声明每种类型的字面意义。对于所有的类型,除了byte和short,我们不需要创建一个变量。我们可以简单地使用字面的值。
For example, when passing arguments to a method, we can pass literals:
例如,当向一个方法传递参数时,我们可以传递字词。
static void literals(int i, long l, double d, float f, char c, String s) {
// do something
}
//Call literals method
literals(1, 123L, 1.0D, 1.0F, 'a', "a");
There are notations to specify the types of literals, so they aren’t used as the default types. In the example above, F is the only mandatory notation.
有一些符号可以指定字面的类型,所以它们并不作为默认的类型使用。在上面的例子中,F是唯一的强制性符号。
Surprisingly, issues arise for byte and short types:
令人惊讶的是,byte和short类型出现问题。
static void shortAndByteLiterals(short s, byte b) {
// do something
}
//Call the method
shortAndByteLiterals(1, 0); // won't compile
Despite this limitation, there are 2 solutions.
尽管有这种限制,但有2种解决方案。
The first solution is to use some variables we previously declared:
第一个解决方案是使用我们之前声明的一些变量。
short s = 1;
byte b = 1;
shortAndByteLiterals(s, b);
Another option’s to cast the literal value:
另一个选择是铸造字面价值。
shortAndByteLiterals((short) 1, (byte) 0);
This is necessary in order to make the compiler use the appropriate type.
这是必要的,以便使编译器使用适当的类型。
5. Conclusion
5.总结
In this article, we looked at the different ways of specifying and using literals in Java.
在这篇文章中,我们看了在Java中指定和使用字面意义的不同方法。