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

I want to know which method is faster?

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

Is there a difference in performance or memory between the two approaches?

I saw the difference between parseInt and valueOf in java? , but this does not explain the differences in performance.

+4
source share
4 answers

I would not look at performance. The API says that Integer.valueOf(String) interpreted as if it were passed in Integer.parseInt(String) , except that it was wrapped in Integer . I would see what you need: Integer or int .

Integer.valueOf returns Integer .

Integer.parseInt returns int .

+15
source

Integer.valueOf() uses Integer.parseInt() internally, and valueOf returns an Integer Object, while parseInt() returns an int . Therefore, parseInt() is faster.

+1
source

Integer.valueOf(String string) returns the newly created completed object .

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

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

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

0
source

valueOf(String) returns a new Integer() object, while parseInt(String) returns an int primitive.

0
source

All Articles