简体   繁体   English

为什么Long.parseLong(String s)不会将尾随的“L”视为有效?

[英]Why Long.parseLong(String s) won't consider a trailing “L” as valid?

Long myLong = Long.parseLong("1L") // Throws NumberFormatException

My best guess is that an additional check like that would cause an overhead which would only come into play ~1% (or less) of the time anyone calls parseLong with a trailing "L" character. 我最好的猜测是,这样的额外检查会导致开销只会在任何人用一个尾随的“L”字符调用parseLong的时间内发挥〜1%(或更少)。

But is there perhaps a different reason behind this "omission"? 但这种“遗漏”背后可能有不同的原因吗?

It is defined so in the method java.lang.Long.parseLong(String): 它在方法java.lang.Long.parseLong(String)中定义:

The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign ... 字符串中的字符必须都是十进制数字,除了第一个字符可能是ASCII减号...

The trailing L (or l ) is only relevant to the Java compiler to distinguish int literals from long literals. 尾部L (或l )仅与Java 编译器相关,以区分int文字和long文字。 It's not a universal marker for long values, so accepting (and ignoring) it would be more of a weird side effect, not to mention a source for bugs. 它不是长值的通用标记,因此接受(并忽略)它会更像是一种奇怪的副作用,更不用说bug的来源了。

Thinking about the use case of Long.parseLong("1L"); 关于Long.parseLong("1L");用例的思考Long.parseLong("1L"); is you got long value in the form of String with l at the end. 你最后是以String形式获得的长值l

Now how can you get this value in real life applications, consider GUI or console application, conversion of number String to actual type will not have suffix l or d . 现在如何在现实应用中获得此值,考虑GUI或控制台应用程序,将数字String转换为实际类型将不具有后缀ld From user input to database value suffix l is not acceptable for number input. 从用户输入到数据库值后缀l对于数字输入是不可接受的。 You will never get String value as 1L from the business logic for the conversion to actual Long value. 对于转换为实际Long值的业务逻辑,您永远不会将String值作为1L

Surprisingly, on the other hand Double myDouble = Double.parseDouble("1d"); 令人惊讶的是,另一方面Double myDouble = Double.parseDouble("1d"); will work fine because sun.misc.FloatingDecimal managed to parse with suffix d in number because double value may contain other characters like E or e in it for exponential term. 将工作正常,因为sun.misc.FloatingDecimal设法用数字后缀d进行解析,因为double值可能包含其他字符,如Ee ,用于指数项。

The L in 1L indicates to the Java compiler that the value is a long literal . 1LL表示Java编译器的值是long 文字 It only has meaning in the Java source code for disambiguation with 1 (an int literal). 它只对Java源代码有意义,用于消除歧义1 (一个int文字)。

When you use Long.parseLong(String) with the string "1L" , it is a runtime error, because L is not a valid numerical digit in base 10. There is also no need for disambiguation, as you have already explicitly declared you want a Long as the result. 当您使用字符串"1L" Long.parseLong(String)时,它是一个运行时错误,因为L不是基数10中的有效数字。也没有必要消除歧义,因为您已经明确声明了您想要的结果很Long

Or more formally, the documentation of Long.parseLong(String) specifies: 或者更正式地说, Long.parseLong(String)的文档指定:

Parses the string argument as a signed decimal long. 将字符串参数解析为带符号的十进制长度。 The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign ' - ' ( \- ') to indicate a negative value or an ASCII plus sign ' + ' (' \+ ') to indicate a positive value. 字符串中的字符必须都是十进制数字,除了第一个字符可以是ASCII减号' - '( \- ')表示负值或ASCII加号' + '(' \+ ')到表示正值。 The resulting long value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseLong(java.lang.String, int) method. 返回得到的long值,就像将参数和基数10作为parseLong(java.lang.String, int)方法的参数给出一样。

Note that neither the character L (' \L ') nor l (' \l ') is permitted to appear at the end of the string as a type indicator, as would be permitted in Java programming language source code. 请注意,字符L (' \L ')和l (' \l ')都不允许作为类型指示符出现在字符串的末尾,这在Java编程语言源代码中是允许的。

This last paragraph explicitly documents it is not allowed. 最后一段明确说明不允许这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM