gpt4 book ai didi

Java Short.parseShort 和 Short.valueOf 之间的区别

转载 作者:行者123 更新时间:2023-11-30 06:34:58 27 4
gpt4 key购买 nike

我最近偶然发现了another question在 Stack Overflow 上,有人建议使用 Short.parseShort(String s) 和另一个 Short.valueOf(String s)。

我自己都尝试过,发现功能上和 official Documentation 没有什么区别。也没有真正帮助我:

Short.parseShort: Parses the string argument as a signed decimal short. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value. The resulting short value is returned, exactly as if the argument and the radix 10 were given as arguments to the parseShort(java.lang.String, int) method.

Short.valueOf: Returns a Short object holding the value given by the specified String. The argument is interpreted as representing a signed decimal short, exactly as if the argument were given to the parseShort(java.lang.String) method. The result is a Short object that represents the short value specified by the string.

两者都接受附加参数 radix ,并且都抛出 NumberFormatException .

它们看起来是相同的,但如果是这样的话,为什么两者都存在?

最佳答案

valueOf 在内部使用 parseShort 并另外将值包装在装箱类型 Short 中:

public static Short valueOf(String s, int radix)
throws NumberFormatException {
return valueOf(parseShort(s, radix));
}

关于Java Short.parseShort 和 Short.valueOf 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45367089/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com