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.

6
  • 2
    See [this][1]. Please search google and SO before asking questions. [1]: stackoverflow.com/questions/508665/… Commented Dec 18, 2012 at 12:44
  • you can see stackoverflow.com/questions/7355024/… Commented Dec 18, 2012 at 12:44
  • 1
    My question is about performance. not about difference between these to methods. Commented Dec 18, 2012 at 12:47
  • 1
    you should reopen the question. because it is about performance that is not discussed in mentioned answers. Commented Dec 18, 2012 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. Commented Dec 18, 2012 at 14:24

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

7

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

Comments

2

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

1 Comment

This can be easily be shown to be false by running System.out.println(Integer.valueOf ( "1" ) == Integer.valueOf ( "1" ));
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");

6 Comments

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?
why are so much concern, which one faster, even they are not same. I mean, their return type are different.
@PeteKirkham please refer kathy sierra book (SCJP Guide).And, i believe, It doesn't mean that, it means, it return Integer class object.
@PeteKirkham, now is this ok ?? or still require more improvement ??
@PeteKirkham, either tell me what's wrong in my answer or undo your downvote.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.