The release of JDK1.7 has introduced some useful features, although most of them are syntactic sugars, but still greatly improves the readability and quality of the code. One feature is the underscore of literal constant numbers. Starting with Java7, you can write long integer numbers like 10000000000 in your Java code into a more readable 10_000_000_000. An important reason to underline in literal constant numbers is to avoid subtle errors that are difficult to find by looking at the code. In contrast to 10000000000 and 1000000000, it's hard to find a 0 or more 0 less, but not for 10_000_000_000 and 1_000_000_000. So if you're working with large numbers in the Java source, you can add underscores to the numbers to improve readability . When using the note: The literal constant number is underlined in a certain rule, the underscore can only be between the numbers, at the beginning or end of the number must not use underscores. In the following sections of this section, we will learn how to use underscores in literal constant numbers and the rules for using them in literal constant numbers.
How to use underline for numbers in Java
As I said before, this is just a syntactic sugar, much like the implementation of strings in a switch scenario, which is also accomplished with the help of a compiler. During compilation, the compiler removes the underscore and assigns the actual number to the variable. For example, 10_000_000 will be converted to 10000000 during compilation. Since the CPU has no pressure to deal with long numbers, we don't have to worry about the poor people who have trouble dealing with long numbers. This feature is especially useful in dealing with large amounts of money, credit card numbers, bank accounts, and other banking and financial areas that require long accounts. Although it is frustrating to write sensitive information in a Java file, we should never do this at the time of coding. But using underscores in numbers makes our life easier than ever.
Rules for underlining numbers in Java
The Java encoding language has strict rules for adding underscores to numeric literals. As mentioned above, you can only underline between numbers. You can't use a number to start with an underscore, or an underscore to end. Here are some other places that cannot be underlined on numeric literals:
- At the beginning or end of a number
- Decimal attachment to floating-point numbers
- The front of the F or L subscript
- When the numeric literal is of type string
Here are some examples to show which places are added to the numbers underline valid, which places the numbers underline invalid
1234567891011121314 |
float pi1 = 3_.1415F;
// 无效的; 不能在小数点之前有下划线
float pi2 =
3
._1415F;
// 无效的; 不能在小数点之后有下划线
long socialSecurityNumber1 = 999_99_9999_L;
//无效的,不能在L下标之前加下划线
int a1 = _52;
// 这是一个下划线开头的标识符,不是个数字
int a2 = 5_2;
// 有效
int a3 = 52_;
// 无效的,不能以下划线结尾
int a4 = 5_______2;
// 有效的
int a5 = 0_x52;
// 无效,不能在0x之间有下划线
int a6 = 0x_52;
// 无效的,不能在数字开头有下划线
int a7 = 0x5_2;
// 有效的 (16进制数字)
int a8 = 0x52_;
// 无效的,不能以下划线结尾
int a9 = 0_52;
// 有效的(8进制数)
int a10 = 05_2;
// 有效的(8进制数)
int a11 = 052_;
// 无效的,不能以下划线结尾
|
Here are some other examples that are underlined in numbers:
12345678 |
long creditCardNumber = 6684_5678_9012_3456L;
// 在编码的时候,最好永远不要这么做
long socialSecurityNumber = 333_99_9999L;
// 在编码的时候,最好永远不要这么做
float pi =
3
.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
|
With underscores, you'll find your code is more readable than before. Incidentally, in Java you should always use L to represent a long integer number. Although it is legal to use the lowercase l to denote a long integer, he looks too much like 1, so it should never be used. Tell me if you can find the difference between 12l and 121, I guess there are not many people to find. But between 12L and 121?
In short, you should develop the habit of using underscores in numbers , especially for long integers, which can increase readability. I know that this function only works from Java1.7, and has not been widely used. But given the status of Java1.8, I expect Java8 to spread more rapidly and extensively in the Java community than Java7.
Why Java7 start using underscores in numbers