parseInt()如何不同于valueOf() ?
它们似乎对我做了完全相同的事情(也适用于parseFloat(), parseDouble(), parseLong()等,它们与Long.valueOf(字符串)有什么不同?
另外,按照惯例,哪一个更可取,更常用呢?
parseInt()如何不同于valueOf() ?
它们似乎对我做了完全相同的事情(也适用于parseFloat(), parseDouble(), parseLong()等,它们与Long.valueOf(字符串)有什么不同?
另外,按照惯例,哪一个更可取,更常用呢?
当前回答
查看Java源代码:valueOf使用parseInt:
/**
* Parses the specified string as a signed decimal integer value.
*
* @param string
* the string representation of an integer value.
* @return an {@code Integer} instance containing the integer value
* represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value.
* @see #parseInt(String)
*/
public static Integer valueOf(String string) throws NumberFormatException {
return valueOf(parseInt(string));
}
parseInt返回int(不是整数)
/**
* Parses the specified string as a signed decimal integer value. The ASCII
* character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of an integer value.
* @return the primitive integer value represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value.
*/
public static int parseInt(String string) throws NumberFormatException {
return parseInt(string, 10);
}
其他回答
如果你检查Integer类,你会发现valueof调用parseInt方法。最大的区别是当你调用valueof API时缓存。如果值在-128到127之间,它会缓存。更多信息请参见下面的链接
http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html
查看Java源代码:valueOf使用parseInt:
/**
* Parses the specified string as a signed decimal integer value.
*
* @param string
* the string representation of an integer value.
* @return an {@code Integer} instance containing the integer value
* represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value.
* @see #parseInt(String)
*/
public static Integer valueOf(String string) throws NumberFormatException {
return valueOf(parseInt(string));
}
parseInt返回int(不是整数)
/**
* Parses the specified string as a signed decimal integer value. The ASCII
* character \u002d ('-') is recognized as the minus sign.
*
* @param string
* the string representation of an integer value.
* @return the primitive integer value represented by {@code string}.
* @throws NumberFormatException
* if {@code string} cannot be parsed as an integer value.
*/
public static int parseInt(String string) throws NumberFormatException {
return parseInt(string, 10);
}
parse*变体返回基本类型,valueOf版本返回对象。我相信valueOf版本还将使用内部引用池为给定值返回SAME对象,而不仅仅是具有相同内部值的另一个实例。
好吧,Integer.valueOf(String)的API确实说过,String的解释就像给Integer.parseInt(String)一样。然而,valueOf(String)返回一个新的Integer()对象,而parseInt(String)返回一个原语int。
如果你想享受Integer.valueOf(int)的潜在缓存优势,你也可以使用这个讨厌的东西:
Integer k = Integer.valueOf(Integer.parseInt("123"))
现在,如果你想要的是对象而不是原语,那么使用valueOf(String)可能比用parseInt(String)创建一个新对象更有吸引力,因为前者在Integer, Long, Double等中始终存在。
整数。parseInt可以只返回int作为本机类型。
整数。valueOf实际上可能需要分配一个Integer对象,除非该整数恰好是预分配的整数之一。这样成本更高。
如果你只需要本地类型,使用parseInt。如果你需要一个对象,使用valueOf。
此外,由于这种潜在的分配,自动装箱实际上在各方面都不是一件好事。它可以使事情变慢。