Numeric literals with underscores
Numerical literals are definitely eye strainers. I am sure you would start counting the zeroes like me if you’ve been given a number with, say, ten zeros. It’s quite error prone and cumbersome to identify a literal if it’s a million or a billion unless you count the places from right to left. Not anymore. Java 7 introduced underscores in identifying the places. For example, you can declare 1000 as shown below:
int thousand = 1_000;
or 1000000 (one million) as follows
int million = 1_000_000
Note that binary literals are also introduced in this release too — for example “0b1” — so developers don’t have to convert them to hexadecimals any more.
本文介绍Java7引入的新特性,即在数字字面量中使用下划线来提高可读性,例如可以将1000写为1_000。此外还介绍了在同一版本中引入的二进制字面量,如0b1,这使得开发者不再需要将其转换为十六进制。
752

被折叠的 条评论
为什么被折叠?



