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

后端 未结 4 1004
执笔经年
执笔经年 2021-02-06 01:41

I want to know which method is faster?

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

Is there any Performance or M

相关标签:
4条回答
  • 2021-02-06 01:59

    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.

    0 讨论(0)
  • 2021-02-06 02:03

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

    0 讨论(0)
  • 2021-02-06 02:09

    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");

    0 讨论(0)
  • 2021-02-06 02:21

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

    0 讨论(0)
提交回复
热议问题