我看到NumberFormat有两种非常相似的方法:
1)
abstract Number parse(String source, ParsePosition parsePosition)
Returns a Long if possible (e.g., within the range [Long.MIN_VALUE, Long.MAX_VALUE] and with no decimals), otherwise a Double.
2)
Object parseObject(String source, ParsePosition pos)
Parses text from a string to produce a Number.
在存在 parse(s,ParseIndex)
的情况下,需要将 parseObject(..)
转换为所需的输出?
我知道第一个方法会抛出 ParseException
而后者则不会。
只是想知道为什么需要这两种非常相似的方法。
提前致谢。
最佳答案
关于 JDK 最常见的提示之一是为什么 Integer.parseInt 和类似的方法不能用于在不涉及昂贵的异常机制的情况下验证字符串。 NumberFormat
似乎通过提供一种可以通过返回 null
来表示失败的方法来回答这个提示。
关于java - 为什么NumberFormat中需要以下2个方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18442203/