java se7 if_Java SE7新特性之在数值型的字面值中使用下划线
在Java SE 7及后續(xù)版本中, 數(shù)值型的字面值中的數(shù)字之間可以出現(xiàn)任何數(shù)量的下劃線。例如,這個(gè)特性可以讓你將數(shù)值型的字面值中的數(shù)字分隔成組,這樣可以提高代碼的可讀性。
比如, 如果你的代碼包含有許多位的數(shù)字, 你可以使用下劃線將這些數(shù)字分成三組, 和使用標(biāo)點(diǎn)符號(hào)(逗號(hào)或者空格)作為分隔符一樣。
下面的例子展示了在數(shù)值型的字面值中可以使用下劃線的其它的一些方式:
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;
你只能將下劃線置于數(shù)字之間; 以下地方不能放置下劃線:
數(shù)字的開頭或結(jié)尾
浮點(diǎn)數(shù)中靠近小數(shù)點(diǎn)的位置
F?或?L??后綴之前
期望放置一串?dāng)?shù)字的地方
下面的例子展示了數(shù)值型的字面值中有效和無效的下劃線位置 (高亮的) :
float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent to a decimal point
float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent to a decimal point
long socialSecurityNumber1
= 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffix
int x1 = _52; // This is an identifier, not a numeric literal
int x2 = 5_2; // OK (decimal literal)
int x3 = 52_; // Invalid; cannot put underscores at the end of a literal
int x4 = 5_______2; // OK (decimal literal)
int x5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix
int x6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number
int x7 = 0x5_2; // OK (hexadecimal literal)
int x8 = 0x52_; // Invalid; cannot put underscores at the end of a number
int x9 = 0_52; // OK (octal literal)
int x10 = 05_2; // OK (octal literal)
int x11 = 052_; // Invalid; cannot put underscores at the end of a number
總結(jié)
以上是生活随笔為你收集整理的java se7 if_Java SE7新特性之在数值型的字面值中使用下划线的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html九宫格布局原理,了解CSS九宫格
- 下一篇: Android 解压 Zip 压缩文件