数字文字中的 Java 7 下划线 [英] Java 7 underscore in numeric literals

查看:36
本文介绍了数字文字中的 Java 7 下划线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们必须使用 _ 来分隔数字中的数字时,我不明白以下无法使用它的情况:

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:

在需要一串数字的位置

(如 JDK7 指南中所述此处)

(as documented in the JDK7 guide here)

一些例子?

推荐答案

您不必必须使用_",您可以.提案中给出的示例是信用卡号、电话号码,或者只是在代码中使用分隔符才有意义的数字.

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.

对于在需要一串数字的位置",它只是在应该以数字开头(或结尾)的地方.以下是一些示例.

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)

<小时>

资源:

这篇关于数字文字中的 Java 7 下划线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆