Home » Java » Difference between parseInt and valueOf in java?

Difference between parseInt and valueOf in java?

Posted by: admin November 2, 2017 Leave a comment

Questions:

What’s the difference between these two methods? 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) ?

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

Answers:

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

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

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.

Questions:
Answers:

From this forum:

parseInt() returns primitive integer
type (int), whereby valueOf returns
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 intValue is an instance method
whereby parseInt is a static method.

Questions:
Answers:
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 Integer instance, which can cause confusing results where the result of == tests seem intermittently correct. Before autoboxing there could be a difference in convenience, after java 1.5 it doesn’t really matter.

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

Questions:
Answers:

Look at Java sources: valueOf is using 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 returns 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);
}

Questions:
Answers:

Integer.parseInt can just return int as native type.

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

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

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

Questions:
Answers:

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.

your code is same as

int abc = new Integer(123);

Questions:
Answers:

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.

Questions:
Answers:

Since valueOf return a new Integer object why the code below is correct?

String base5String = "230";
int result = Integer.valueOf(base5String);

Questions:
Answers:

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

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

Questions:
Answers:

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

Questions:
Answers:
  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.

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.