带下划线的数字文字如何在 java 中工作以及为什么将其添加为 jdk 1.7 的一部分

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19806632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:20:37  来源:igfitidea点击:

How Numeric literal with underscore works in java and why it was added as part of jdk 1.7

javajava-7

提问by Dark Knight

Can somebody please explain to me why this feature was added in JDK 7 and how it works?

有人可以向我解释为什么在 JDK 7 中添加了这个特性以及它是如何工作的吗?

While going through the new features of JDK 7, I found following code.

在浏览 JDK 7 的新特性时,我发现了以下代码。

int i;
//Java 7 allows underscore in integer
i=3455_11_11;

采纳答案by Pradeep Simha

This is used to group the digits in your numeric (say for example for credit card etc)

这用于对数字中的数字进行分组(例如信用卡等)

From Oracle Website:

甲骨文网站

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator.

在 Java SE 7 及更高版本中,任意数量的下划线字符 (_) 可以出现在数字文本中数字之间的任何位置。例如,此功能使您能够将数字文字中的数字组分开,从而提高代码的可读性。

例如,如果您的代码包含具有多个数字的数字,您可以使用下划线字符将数字分成三组,类似于使用逗号或空格等标点符号作为分隔符的方式。

To conclude, it's just for a sake of readability.

总而言之,这只是为了可读性。

回答by Juned Ahsan

JDK 7 _for numeric literals feature is only for the sake of readability. As per docs:

JDK 7_的数字文字功能只是为了可读性。根据文档

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

在 Java SE 7 及更高版本中,任意数量的下划线字符 (_) 可以出现在数字文本中数字之间的任何位置。例如,此功能使您能够将数字文字中的数字组分开,从而提高代码的可读性。

回答by Maroun

See Underscores in Numeric Literals:

请参阅数字文字中的下划线

In Java SE 7 and later, any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code.

在 Java SE 7 及更高版本中,任意数量的下划线字符 (_) 可以出现在数字文本中数字之间的任何位置。例如,此功能使您能够将数字文字中的数字组分开,从而提高代码的可读性。

Try this:

尝试这个:

int num = 111_222;
System.out.println(num); //Prints 111222

This feature was added due to the fact that long numbers can be hard to read sometimes, so instead of counting how many "zeros" a number has to figure out if it's a million or one hundred thousand, you can do:

添加此功能是因为有时长数字可能难以阅读,因此与其计算一个数字有多少个“零”来确定它是一百万还是十万,您可以执行以下操作:

int myNum = 1_000_000;

int myNum = 1_000_000;

Now it's easy to see that there is two groups of 3 zeros, and clearly the number is million. Compare it with:

现在很容易看出有两组 3 个零,显然数字是百万。与它进行比较:

int myNum = 1000000;

int myNum = 1000000;

Admit.. here you had to count each zero..

承认..在这里你必须计算每个零..

回答by Johannes H.

The underscore is completely ignored in Integer literals. It may be added to, for example, group digits in long numbers in groups of 3 (like you do in texts).

整数文字中完全忽略下划线。例如,可以将其添加到以 3 为一组的长数字中分组数字(就像您在文本中所做的那样)。