java – Wierd语法 – 数字之间的下划线

参见英文答案 > Java 7 underscore in numeric literals8个
按照惯例,当我遇到这个问题时,我一直在研究更多的UIL Java练习表:
int _ = 8;
System.out.println(5_5);

问题是“以下代码片段的输出是什么?”

我的第一个猜测是语法错误,但正确的响应实际上是55.

为什么是这样?

解决方法

从Java 7开始,您可以在数字之间加下划线,以提高可读性:

JLS – Section 3.10.1JLS Section 3.10.2

Underscores are allowed as separators between digits that denote the integer.

对于浮点字面值:

Underscores are allowed as separators between digits that denote the whole-number part,and between digits that denote the fraction part,and between digits that denote the exponent.

例如,现在可以将1000000000写为1_000_000_000.所以,对眼睛来说更好,不是吗.

同样,你可以写 – 0x7fff_ffff,0b0111_1111.

关于变量名,_是有效的变量名.根据Java标准,变量名称应以$,_或字母开头.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...