Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which one is faster? Integer.valueOf(String string) or Integer.parseInt(String string)?

Tags:

java

android

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.

like image 381
Linaina Avatar asked Dec 18 '12 12:12

Linaina


People also ask

What is the difference between integer parseInt () and integer valueOf ()?

valueOf() returns an Integer object while Integer. parseInt() returns a primitive int. Both String and integer can be passed a parameter to Integer.

What is the difference between Parse and valueOf in java?

Difference between parseInt vs valueOf in Java parseInt() method simply parses the String which is passed to it and returns primitive int. As we can see from the above code, valueOf() method passes the String to the parseInt() method which performs the actual conversion of String and returns the Wrapper Integer.

What happens if you integer parseInt a string?

The parseInt function converts its first argument to a string, parses that string, then returns an integer or NaN . If not NaN , the return value will be the integer that is the first argument taken as a number in the specified radix .

What is the use of integer valueOf string?

valueOf(String str) is an inbuilt method which is used to return an Integer object, holding the value of the specified String str. Parameters: This method accepts a single parameter str of String type that is to be parsed.


1 Answers

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.

like image 79
Tom Verelst Avatar answered Sep 30 '22 14:09

Tom Verelst