Reputation:
If I have a Long (not the primitive) whats the best way to convert it to a string. By best, I mean fastest.
Currently, I'm doing this.
Long testLong = 123456L;
return new StringBuilder()
.append(PREFIX)
.append("_")
.append(testLong)
.toString();
is String.valueOf(testLong)
or testLong.toString()
better?
Upvotes: 0
Views: 657
Reputation:
.append(testLong)
calls .append(String.valueOf(testLong))
.
.append(String.valueOf(testLong))
calls .append((testLong == null) ? "null" : testLong.toString())
.append(testLong.toString())
calls .append(Long.toString(testLong.value))
(where value
is the boxed long
), which creates a new String
.append(testLong.longValue())
does not create a new String
, instead, it writes directly to the StringBuilder
byte array.
Therefore the latter is the fastest, if you know that the Long
will never be null
:
Long testLong = 123456L;
return new StringBuilder()
.append(PREFIX)
.append("_")
.append(testLong.longValue())
.toString();
Upvotes: 1