如何使用下划线的Numeric文字在java中工作,以及为什么它作为jdk 1.7的一部分添加 [英] How Numeric literal with underscore works in java and why it was added as part of jdk 1.7

查看:92
本文介绍了如何使用下划线的Numeric文字在java中工作,以及为什么它作为jdk 1.7的一部分添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释一下,为什么在JDK 7中添加了这个功能以及它是如何工作的?

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

在浏览JDK 7新功能时,我发现了以下代码。

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

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


推荐答案

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

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

来自 Oracle网站


在Java SE 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.

例如,如果您的代码包含数字许多数字,你可以
使用下划线字符来分隔三个数组中的数字,
类似于使用逗号或逗号(b $ b空格)作为分隔符的标点符号。

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.

总而言之,这只是为了便于阅读。

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

这篇关于如何使用下划线的Numeric文字在java中工作,以及为什么它作为jdk 1.7的一部分添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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