为什么要在Java SE 7的数字中使用下划线-在数字文字中使用下划线
JDK 1.7發(fā)行版引入了幾個(gè)有用的功能,盡管其中大多數(shù)都是語(yǔ)法糖,但使用該功能可以大大提高可讀性和代碼質(zhì)量。 這樣的功能之一是在數(shù)字文字中引入下劃線(xiàn) 。 從Java 7開(kāi)始,您可以在Java源代碼中向可讀性更高的10_000_000_000寫(xiě)一個(gè)長(zhǎng)數(shù)字,例如10000000000。 在數(shù)字文字中使用下劃線(xiàn)的最重要原因之一是避免了細(xì)微的錯(cuò)誤,而這些細(xì)微的錯(cuò)誤很難通過(guò)查看代碼來(lái)找出。 很難注意到在10000000000和1000000000之間缺少的零或多余的零,而不是10_000_000_000和1_000_000_000。 因此,如果您正在使用Java源代碼處理大量數(shù)字,請(qǐng)?jiān)跀?shù)字中使用下劃線(xiàn)以提高可讀性 。 順便說(shuō)一句,在數(shù)字文字中使用下劃線(xiàn)是有規(guī)則的,因?yàn)樗鼈円彩菢?biāo)識(shí)符中的有效字符,因此您只能在數(shù)字之間使用它們,而不能在數(shù)字文字的開(kāi)頭或數(shù)字文字的末尾使用下劃線(xiàn)。 在下一部分中,我們將學(xué)習(xí)如何實(shí)現(xiàn)數(shù)字文字中的下劃線(xiàn)以及如何在數(shù)字文字中使用下劃線(xiàn)。
如何在Java中實(shí)現(xiàn)數(shù)字下劃線(xiàn)
就像我說(shuō)的那樣,它是一種語(yǔ)法糖,就像在切換情況下如何實(shí)現(xiàn)String一樣,這也是在編譯器的幫助下實(shí)現(xiàn)的。 在編譯時(shí),編譯器會(huì)刪除這些下劃線(xiàn)并將實(shí)際數(shù)字放入變量中。 例如10_000_000將在編譯時(shí)轉(zhuǎn)換為10000000。 由于CPU處理長(zhǎng)數(shù)字串沒(méi)有問(wèn)題,這對(duì)他很有趣,所以我們不必理會(huì),就是我們這個(gè)貧窮的人遇到了處理長(zhǎng)數(shù)字的問(wèn)題。 此功能對(duì)于銀行和金融領(lǐng)域應(yīng)用程序特別有用,該應(yīng)用程序處理大筆錢(qián),信用卡號(hào),銀行帳號(hào)和其他處理較長(zhǎng)ID的域。 盡管強(qiáng)烈建議不要在Java文件中寫(xiě)入敏感數(shù)據(jù),并且絕對(duì)不要在生產(chǎn)代碼中這樣做,但帶下劃線(xiàn)的數(shù)字比以前容易得多。
Java中在數(shù)字中使用下劃線(xiàn)的規(guī)則
Java編程語(yǔ)言對(duì)于在數(shù)字文字中使用下劃線(xiàn)具有嚴(yán)格的規(guī)則集。 如前所述,您只能在數(shù)字之間使用它們。 您不能以下劃線(xiàn)開(kāi)頭或以下劃線(xiàn)結(jié)尾。 這是更多地方,您不能在數(shù)字文字中使用下劃線(xiàn):
這是幾個(gè)示例,顯示了數(shù)字文字中下劃線(xiàn)的一些有效和無(wú)效用法
float pi1 = 3_.1415F; // Invalid; cannot put underscores adjacent (before) to a decimal point float pi2 = 3._1415F; // Invalid; cannot put underscores adjacent (after) to a decimal point long socialSecurityNumber1 = 999_99_9999_L; // Invalid; cannot put underscores prior to an L suffixint a1 = _52; // This is an identifier, not a numeric literal, starts with underscore int a2 = 5_2; // OK (decimal literal) int a3 = 52_; // Invalid; cannot put underscores at the end of a literal int a4 = 5_______2; // OK (decimal literal)int a5 = 0_x52; // Invalid; cannot put underscores in the 0x radix prefix int a6 = 0x_52; // Invalid; cannot put underscores at the beginning of a number int a7 = 0x5_2; // OK (hexadecimal literal) int a8 = 0x52_; // Invalid; cannot put underscores at the end of a numberint a9 = 0_52; // OK (octal literal) int a10 = 05_2; // OK (octal literal) int a11 = 052_; // Invalid; cannot put underscores at the end of a number這是在數(shù)字文字中使用下劃線(xiàn)的更多示例
long creditCardNumber = 6684_5678_9012_3456L; // Never do it on production code long socialSecurityNumber = 333_99_9999L; // Never, Ever do it on production code 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ù)字下劃線(xiàn)相比,代碼更具可讀性。 順便說(shuō)一句,在Java中始終使用L表示長(zhǎng)文字。 盡管使用小寫(xiě)字母l是合法的,但您永遠(yuǎn)不要將其與數(shù)字一起使用,因?yàn)樗雌饋?lái)與數(shù)字1完全相似。請(qǐng)告訴我您是否能找出12l和121之間的差異,我想不是很多。 12L和121怎么樣?
簡(jiǎn)而言之,請(qǐng)始終在數(shù)字中使用下劃線(xiàn) ,尤其是使用長(zhǎng)數(shù)字時(shí),應(yīng)使其更具可讀性。 我知道此功能僅在Java 1.7中可用,并且尚未廣泛使用,但是考慮到Java 8配置文件,我希望Java 8將比Java 7更快,更廣泛地被社區(qū)采用。
翻譯自: https://www.javacodegeeks.com/2014/03/why-use-underscore-in-numbers-from-java-se-7-underscore-in-numeric-literals.html
總結(jié)
以上是生活随笔為你收集整理的为什么要在Java SE 7的数字中使用下划线-在数字文字中使用下划线的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 系统垃圾bat怎么清理 Win7一键清理
- 下一篇: Java中的硬件事务性内存,或者为什么同