views:

7106

answers:

7

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?

+12  A: 

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.

Michael Haren
Worth mentioning: 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. This means that given two Longs returned in this way, a.equals(b) == true and a == b is true
basszero
As proven further down, You are correct for the String versions, I was thinking of the primitive versions. Long.valueOf(5) will always return the same object. String versions return new objects, primitive versions return the same objects
basszero
@bassezero. Also, that pool has a limit. I think it was -127 to 127.
OscarRyz
The size of the reference pool is a true example of an implementation detail; it could even be increased in size in a patch release, and you should never *rely* on it for anything.
Donal Fellows
+5  A: 
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.

João da Silva
valueOf() can return the same object for successive calls with the same argument (and is required to for arguments between -128 and 127 inclusive). new Integer() will always create a new object.
Adam Rosenfield
Which one is used more often? Which one should I use the most?
Click Upvote
If you need an int, use parseInt(), if you need an Integer, use valueOf()
matt b
A: 

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.

basszero
Actually, not quite true. I thought so myself at first, but the Javadocs for Integer.valueOf(String) clearly state that it is equivalent to new Integer(Integer.parseInt(String)). Integer.valueOf(int) does indeed cache, though.
Michael Myers
You are correct for the String versions, I was thinking of the primitive versions. Long.valueOf(5) will always return the same object.
basszero
+9  A: 

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.

Zach Scrivena
Can someone roll this back? I liked the previous answer about Integer, String, Long etc all using valueOf and that's why some people prefer valueOf, and I was going to accept this answer because of that
Click Upvote
@Click Upvote: I've updated my answer as suggested.
Zach Scrivena
A: 

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.

iny
A: 
  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.

A: 

Try this

Integer a = Integer.valueOf("10");
Integer b = Integer.valueOf("10");
if (a.equals(b)) {
System.out.println("Equals");
} else {
System.out.println("Not Equals");
}
if (a == b) {
System.out.println("a == b");
} else {
System.out.println("a != b");
}

If you run this example; the result is

Equals
a != b

So Integer.valueOf() does not return the same object.

Even tools like PMD, findBugs recommend using .valueOf()

Kalyan