Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between String.valueOf(long a) and concatenating ("" + a)

Tags:

java

Which approach is better and why ?

If I write

cmissValue = String.valueOf(callDBDatasource.cMiss());

or

cmissValue = "" + callDBDatasource.cMiss();

What should be the approach?

like image 932
Srinath Ganesh Avatar asked Jul 05 '26 09:07

Srinath Ganesh


2 Answers

"" + callDBDatasource.cMiss();

Will compile to:

new StringBuilder().append("").append(callDBDatasource.cMiss()).toString();

This will create a new object and is therefore significantly slower. See this question: Is string concatenaion really that slow?

like image 92
Lukas Knuth Avatar answered Jul 06 '26 23:07

Lukas Knuth


This will be useful here (section "Converting numbers to Strings"): http://www.odi.ch/prog/design/newbies.php

Shortly:

    String.valueOf(callDBDatasource.cMiss());
like image 27
kyticka Avatar answered Jul 07 '26 00:07

kyticka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!