Using Underscore Characters in Numeric Literals
来源:互联网 发布:手机淘宝买东西步骤 编辑:程序博客网 时间:2024/05/22 06:50
Reprint from the following URL:
http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
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.
The following example shows other ways you can use the underscore in numeric literals:
long creditCardNumber = 1234_5678_9012_3456L;long socialSecurityNumber = 999_99_9999L;float pi = 3.14_15F;long hexBytes = 0xFF_EC_DE_5E;long hexWords = 0xCAFE_BABE;long maxLong = 0x7fff_ffff_ffff_ffffL;byte nybbles = 0b0010_0101;long bytes = 0b11010010_01101001_10010100_10010010;
You can place underscores only between digits; you cannot place underscores in the following places:
- At the beginning or end of a number
- Adjacent to a decimal point in a floating point literal
- Prior to an
F
orL
suffix - In positions where a string of digits is expected
The following examples demonstrate valid and invalid underscore placements (which are highlighted) in numeric literals:
float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal pointfloat pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal pointlong socialSecurityNumber1 = 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffixint x1 = _52; // This is an identifier, not a numeric literalint x2 = 5_2; // OK (decimal literal)int x3 = 52_; // Invalid; cannot put underscores at the end of a literalint x4 = 5_______2; // OK (decimal literal)int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefixint x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a numberint x7 = 0x5_2; // OK (hexadecimal literal)int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number
- Using Underscore Characters in Numeric Literals
- String literals in formulas can't be bigger than 255 characters ASCII
- String literals in formulas can't be bigger than 255 characters ASCII
- java7新特性之Enhanced syntax for numeric literals
- [Python]Converting Between Characters and Numeric Codes
- Literals
- Convert non-numeric data to numeric by using LabelEncoder
- Numeric String Sort in C#
- How do you remove the duplicate characters in a given string without using any additional buffer.
- Underscore
- Underscore
- Recipe 1.2. Converting Between Characters and Numeric Codes
- ISO 8859-1 Characters as Named and Numeric HTML Entities
- In Javascript, what does this underscore mean?
- Special Characters in ToolTip
- Characters Encoding in Linux
- Characters in Regex expression
- Limits on numeric data representation in LotusScript
- php验证码的实现
- kmp算法研究
- wireshark在ubuntu下找不到网卡的解决办法
- 视频格式简析解惑之二——视频编码
- Java的垃圾回收之算法
- Using Underscore Characters in Numeric Literals
- outlasterror
- openCV系列——第7周:在iPhone上运行OpenCV
- 谈谈开发语言
- [Android]实现静默安装APK的两种方法
- 创建window应用程序(win32 非 mfc) 去掉菜单栏
- spring2.0 和 spring2.5的一些小区别
- Android读取联系人的姓名及号码
- 关于linux