11

I want to know which method is faster?

Integer.valueOf(String string) or Integer.parseInt(String string)?

Is there any Performance or Memory difference between the two approaches?

I have seen Difference between parseInt and valueOf in java? but this does not explains difference in terms of performance.

Community
  • 1
  • 1
Linaina
  • 161
  • 1
  • 2
  • 8
  • 2
    See [this][1]. Please search google and SO before asking questions. [1]: http://stackoverflow.com/questions/508665/difference-between-parseint-and-valueof-in-java – Matt Clark Dec 18 '12 at 12:44
  • you can see http://stackoverflow.com/questions/7355024/integer-valueof-vs-integer-parseint – ρяσѕρєя K Dec 18 '12 at 12:44
  • 1
    My question is about performance. not about difference between these to methods. – Linaina Dec 18 '12 at 12:47
  • 1
    you should reopen the question. because it is about performance that is not discussed in mentioned answers. – Linaina Dec 18 '12 at 12:52
  • `Integer.valueOf("1")` is functionally equivalent to `Integer.valueOf(Integer.parseInt("1"))`; alternatively `Integer.parseInt("2")` is equivalent to `Integer.valueOf("2").intValue()`. Because they return different types, there is no situation where one is a direct replacement, so you need to ask about the performance of whichever one gives you the type you actually require, or better how to get the performance you require for a larger chunk of your code. – Pete Kirkham Dec 18 '12 at 14:24
  • @Linaina If you did solve this problem, Accept any of the answers below – Rajeev N B Dec 20 '12 at 04:48

4 Answers4

20

I would not look at performance. The API says that Integer.valueOf(String) is interpreted the same as if it has been passed to Integer.parseInt(String), except it is wrapped into an Integer. I would look at what you need: an Integer or an int.

Integer.valueOf returns an Integer.

Integer.parseInt returns an int.

Tom Verelst
  • 15,324
  • 2
  • 30
  • 40
7

Integer.valueOf() uses Integer.parseInt() internally and valueOf returns Integer Object whereas parseInt() returns int. So parseInt() is faster.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Rajeev N B
  • 1,365
  • 2
  • 12
  • 24
2

valueOf(String) returns a new Integer() object whereas parseInt(String) returns a primitive int.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
Aamir Shah
  • 4,473
  • 8
  • 21
  • 28
  • This can be easily be shown to be false by running `System.out.println(Integer.valueOf ( "1" ) == Integer.valueOf ( "1" ));` – Pete Kirkham Dec 31 '12 at 10:55
0

Integer.valueOf(String string) returns a newly created wrapped object.

Integer i = Integer.valueOf("5");

Integer.parseInt(String string) returns the named primitive.

int i = Integer.parseInt("5");

Ravi
  • 30,829
  • 42
  • 119
  • 173
  • So if I invoke Integer.valueOf("2") from MyClass, then it returns a new instance of MyClass, because that is the type which invoked the method? – Pete Kirkham Dec 18 '12 at 12:46
  • why are so much concern, which one faster, even they are not same. I mean, their return type are different. – Ravi Dec 18 '12 at 12:49
  • @PeteKirkham please refer kathy sierra book (SCJP Guide).And, i believe, It doesn't mean that, it means, it return Integer class object. – Ravi Dec 18 '12 at 12:51
  • @PeteKirkham, now is this ok ?? or still require more improvement ?? – Ravi Dec 18 '12 at 12:54
  • @PeteKirkham, either tell me what's wrong in my answer or undo your downvote. – Ravi Dec 18 '12 at 13:04
  • As I pointed out, the type returned is not anything to do with the type which invoked the method. Whatever invokes Integer.valueOf, it always returns an Integer. – Pete Kirkham Dec 18 '12 at 14:16