java中parseInt()和valueOf()的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/508665/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:33:05  来源:igfitidea点击:

Different between parseInt() and valueOf() in java?

java

提问by Click Upvote

How is parseInt()different from valueOf()?

如何parseInt()不同valueOf()

They appear to do exactly the same thing to me (also goes for parseFloat(), parseDouble(), parseLong()etc, how are they different from Long.valueOf(string)?

他们似乎做同样的事情对我来说(也无二parseFloat()parseDouble()parseLong()等等,他们如何不同Long.valueOf(string)

Also, which one of these is preferable and used more often by convention?

此外,按照惯例,其中哪一种更可取且使用频率更高?

采纳答案by Zach Scrivena

Well, the API for Integer.valueOf(String)does indeed say that the Stringis interpreted exactly as if it were given to Integer.parseInt(String). However, valueOf(String)returns a newInteger()object whereas parseInt(String)returns a primitive int.

好吧,for 的 APIInteger.valueOf(String)确实说 的String被解释为就像给Integer.parseInt(String). 但是,valueOf(String)返回一个对象而返回一个原语。newInteger()parseInt(String)int

If you want to enjoy the potential caching benefits of Integer.valueOf(int), you could also use this eyesore:

如果你想享受 潜在的缓存好处Integer.valueOf(int),你也可以使用这个令人眼花缭乱的:

Integer k = Integer.valueOf(Integer.parseInt("123"))

Now, if what you want is the object and not the primitive, then using valueOf(String)may be more attractive than making a new object out of parseInt(String)because the former is consistently present across Integer, Long, Double, etc.

现在,如果你想要的是对象,而不是原始的,然后使用valueOf(String)可能比制作一个新的对象出更有吸引力parseInt(String),因为前者是跨始终存在IntegerLongDouble,等。

回答by Michael Haren

From this forum:

这个论坛

parseInt()returns primitive integer type (int), whereby valueOfreturns java.lang.Integer, which is the object representative of the integer. There are circumstances where you might want an Integer object, instead of primitive type.

Of course, another obvious difference is that intValueis an instance method whereby parseIntis a static method.

parseInt()返回原始整数类型(int),从而valueOf返回 java.lang.Integer,它是整数的对象代表。在某些情况下,您可能需要一个 Integer 对象,而不是原始类型。

当然,另一个明显的区别是intValue是一个实例方法,而parseInt是一个静态方法。

回答by Joao da Silva

Integer.valueOf(s)

is similar to

类似于

new Integer(Integer.parseInt(s))

The difference is valueOf()returns an Integer, and parseInt()returns an int(a primitive type). Also note that valueOf()can return a cached Integerinstance, which can cause confusing results where the result of ==tests seem intermittently correct. Before autoboxingthere could be a difference in convenience, after java 1.5 it doesn't really matter.

不同之处在于valueOf()返回一个Integer,并parseInt()返回一个int(原始类型)。另请注意,valueOf()可以返回缓存的Integer实例,这可能会导致==测试结果间歇性正确的情况下导致混乱的结果。在自动装箱之前,便利性可能有所不同,在 Java 1.5 之后,这并不重要。

Moreover, Integer.parseInt(s)can take primitive datatype as well.

此外,Integer.parseInt(s)也可以采用原始数据类型。

回答by basszero

The parse* variations return primitive types and the valueOf versions return Objects. I believe the valueOf versions will also use an internal reference pool to return the SAME object for a given value, not just another instance with the same internal value.

parse* 变体返回原始类型,而 valueOf 版本返回对象。我相信 valueOf 版本还将使用内部引用池为给定值返回 SAME 对象,而不仅仅是具有相同内部值的另一个实例。

回答by iny

Integer.parseInt can just return int as native type.

Integer.parseInt 可以只返回 int 作为本机类型。

Integer.valueOf may actually need to allocate an Integer object, unless that integer happens to be one of the preallocated ones. This costs more.

Integer.valueOf 实际上可能需要分配一个 Integer 对象,除非该整数恰好是预先分配的对象之一。这成本更高。

If you need just native type, use parseInt. If you need an object, use valueOf.

如果您只需要本机类型,请使用 parseInt。如果您需要一个对象,请使用 valueOf。

Also, because of this potential allocation, autoboxing isn't actually good thing in every way. It can slow down things.

此外,由于这种潜在的分配,自动装箱实际上在各个方面都不是好事。它可以减慢速度。

回答by iny

  1. In case of ValueOf -> it is creating an Integer object. not a primitive type and not a static method.
  2. In case of ParseInt.ParseFloat -> it return respective primitive type. and is a static method.
  1. 在 ValueOf -> 的情况下,它正在创建一个 Integer 对象。不是原始类型,也不是静态方法。
  2. 在 ParseInt.ParseFloat -> 的情况下,它返回各自的原始类型。并且是一个静态方法。

We should use any one depending upon our need. In case of ValueOf as it is instantiating an object. it will consume more resources if we only need value of some text then we should use parseInt,parseFloat etc.

我们应该根据需要使用任何一种。在 ValueOf 的情况下,因为它正在实例化一个对象。如果我们只需要一些文本的值,它会消耗更多资源,那么我们应该使用 parseInt、parseFloat 等。

回答by MAR

Because you might be using jdk1.5+ and there it is auto converting to int. So in your code its first returning Integer and then auto converted to int.

因为您可能正在使用 jdk1.5+,并且它会自动转换为 int。因此,在您的代码中,它首先返回 Integer,然后自动转换为 int。

your code is same as

你的代码是一样的

int abc = new Integer(123);

回答by TheGraduateGuy

If you check the Integer class you will find that valueof call parseInt method. The big difference is caching when you call valueof API . It cache if the value is between -128 to 127 Please find below the link for more information

如果您检查 Integer 类,您会发现调用 parseInt 方法的 valueof。当您调用 valueof API 时,最大的区别是缓存。如果值在 -128 到 127 之间,它会缓存 请在链接下方找到更多信息

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html

回答by Paul Verest

Look at Java sources: valueOfis using parseInt:

查看 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));
}

parseIntreturns int

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);
}

回答by shuaihanhungry

public static Integer valueOf(String s)

public static Integer valueOf(String s)

  1. The argument is interpreted as representing a signed decimal integer, exactly as if the argument were given to the parseInt(java.lang.String) method.
  2. The result is an Integer object that represents the integer value specified by the string.

  3. In other words, this method returns an Integer object equal to the value of: new Integer(Integer.parseInt(s))

  1. 该参数被解释为表示一个带符号的十进制整数,就像将参数提供给 parseInt(java.lang.String) 方法一样。
  2. 结果是一个 Integer 对象,表示由字符串指定的整数值。

  3. 换句话说,此方法返回一个 Integer 对象,其值等于: new Integer(Integer.parseInt(s))