How can one convert a primitive value such as integer or boolean to String object?
Well, there are many ways. There is one way that I have seen so many times:
String strValue = new Integer(intValue).toString();
or
String strValue = new Boolean(booleanValue).toString();
Similarly new instance can be created and the toString() method invoked on classes such as Integer, Long, Float, Double, Byte, Short and Boolean.
The problem is that a boxed primitive is allocated just to call toString() method on it. Each time this is executed, a new instance, new object is created. However, object creation is not necessary and can be avoided. Creating object just for one method call is highly inefficient as the object can be garbage collected on the consecutive line.
The solution
It is more efficient to use the static form of toString() method, which takes the primitive value as a parameter.
String strValue = Integer.toString(intValue);
or
String strValue = Boolean.toString(booleanValue);
Side note
...and please don't do the following
Integer number = ...
String strValue = Integer.toString(number.intValue());
If you already have an instance of integer wrapper class, simply call
String strValue = number.toString();









