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.
valueOf() returns an Integer object while Integer. parseInt() returns a primitive int. Both String and integer can be passed a parameter to Integer.
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.
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 .
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With