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

后端 未结 6 477
萌比男神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:06

    Long.valueOf() should be preferred: it returns cached values of Long for some often-used values instead of constructing a new instance as the constructor does.

    Even if some Java versions don't use a cache, using valueOf() makes it possible in future versions, whereas the constructor will always create a new instance.

提交回复
热议问题