we often come across a scenario where in we need to pass a String representation of a primitive and More often then not we use
WrapperClass.toString() ;
and sometimes we usually write
i + "";
if we check to toString implementation of any wrapper class it creates a new String object every time we call it. and same is also true for primitive + "" (as Concatenation during runtime will create new String Object)
So is there any difference between them or they are just an alternative ways to convert a primitive to a String object;
Personally I like String.valueOf(i)
:
"" + i
- that code expresses string concatenation, which isn't what you're after at all.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With