Difference between Long.valueOf(java.lang.String) and new Long(java.lang.String)?

后端 未结 6 483
萌比男神i
萌比男神i 2020-12-24 13:13

I\'m consolidating code written by two different people and notice that casting a String value into a Long has been done in two different ways.

Coder #1 has done t

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 14:02

    Both do parseLong(String, int) internally (int being radix with value as 10), but valueOf has advantage as documented below:

    If a new Long instance is not required, this method should generally be used in preference to the constructor Long(long), as this method is likely to yield significantly better space and time performance by caching frequently requested values.

提交回复
热议问题