Java 7在数字文字中用下划线表示 [英] Java 7 underscore in numeric literals
问题描述
当我们必须使用_来分隔数字中的数字时,我不理解
以下我无法使用它的情况:
When we must use a _ to separate digits in a number I don't' understand the following case in which I can't use it:
在预期数字字符串
的位置
In positions where a string of digits is expected
(如JDK7指南此处)
一些例子?
推荐答案
你没有拥有使用_,你可以。提案中给出的示例包括信用卡号,电话号码或简单的数字,以便在代码中设置分隔符。
You don't have to use "_", you can. And examples given in the proposal are credit card numbers, phone numbers, or simply numbers for which it makes sense to have a separator in the code.
对于In positions其中一串数字是预期的它只是在它应该以数字开始(或结束)的地方。以下是一些示例。
For the "In positions where a string of digits is expected" it's simply in places where it's supposed to start (or end) with a digit. Here are some examples.
请注意,根据此提案,下划线只能放在数字之间。它们不能自行放置在通常需要一串数字的位置:
Note that according to this proposal, underscores can only be placed between digits. They cannot be placed by themselves in positions where a string of digits would normally be expected:
int x1 = _52; //这是一个标识符,而不是数字文字。
int x1 = _52; // This is an identifier, not a numeric literal.
int x2 = 5_2; // 好。 (十进制文字)
int x2 = 5_2; // OK. (Decimal literal)
int x2 = 52_; //非法。 (下划线必须始终在数字之间)
int x2 = 52_; // Illegal. (Underscores must always be between digits)
int x3 = 5_______2; // 好。 (十进制文字。)
int x3 = 5_______2; // OK. (Decimal literal.)
int x4 = 0_x52; //非法。不能将下划线放在0x基数前缀中。
int x4 = 0_x52; // Illegal. Can't put underscores in the "0x" radix prefix.
int x5 = 0x_52; //非法。 (下划线必须始终在数字之间)
int x5 = 0x_52; // Illegal. (Underscores must always be between digits)
int x6 = 0x5_2; // 好。 (十六进制文字)
int x6 = 0x5_2; // OK. (Hexadecimal literal)
int x6 = 0x52_; //非法。 (下划线必须始终在数字之间)
int x6 = 0x52_; // Illegal. (Underscores must always be between digits)
int x6 = 0x_; //非法。 (删除下划线无效)
int x6 = 0x_; // Illegal. (Not valid with the underscore removed)
int x7 = 0_52; // 好。 (八进制文字)
int x7 = 0_52; // OK. (Octal literal)
int x7 = 05_2; // 好。 (八进制文字)
int x7 = 05_2; // OK. (Octal literal)
int x8 = 052_; //非法。 (下划线必须始终在数字之间)
int x8 = 052_; // Illegal. (Underscores must always be between digits)
资源:
- OpenJDK - Project Coin - PROPOSAL: Underscores in Numbers (Version 2)
- Joe Darcy's blog - Project Coin: Project Coin: Literal Grammar Hackery
这篇关于Java 7在数字文字中用下划线表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!