I want to know the difference in two approaches. There are some old codes on which I'm working now, where they are setting primitive
values to a String
value by concatenating with an empty String ""
.
obj.setSomeString("" + primitiveVariable);
But in this link Size of empty Java String it says that If you're creating a separate empty string for each instance, then obviously that will take more memory.
So I thought of using valueOf
method in String
class. I checked the documentation String.valueOf() it says If the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.
So which one is the better way
obj.setSomeString("" + primitiveVariable);
obj.setSomeString(String.valueOf(primitiveVariable));
The above described process of is done within a List
iteration which is having a size of more than 600, and is expected to increase in future.