Tuesday, February 27, 2007

toString() and primitive values

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();

7 comments:

Anonymous said...

I generally do something like this

String str = ""+ intValue;

I guess your method is more efficient

Anonymous said...

Why not just use String.valueOf()?
You can pass it anything and it will do the right thing

Urban Body said...

Even easier: String.valueOf(anything)

Alex Miller said...

Also, you can just call the overloaded String.valueOf() methods and pass them pretty much anything and it will call these methods for you. Also, the Object version handles the null case.

jed said...

hmmm, I think "highly inefficient" is bit harsh. A small test shows that using Integer.toString(int) is about 8%-10% faster than new Integer(int).toString().

A common myth is that object creation is slow and should be avoided if possible. That was the case in Java 1.2 and before, whereas modern VMs are very efficient at object allocation and new generation objects are very efficiently GC'd.

As always beware pre-optimisation.

Unknown said...

Yes you can do it as Quintin and few other people say:

String.valueOf(anything)

Looking at the source code of the String class reveals that valueOf method either implements the same functionality (boolean) or delegates the call onto the static toString method of the wrapper class.

public static String valueOf(boolean b) {
  return b ? "true" : "false";
}
public static String valueOf(int i) {
  return Integer.toString(i, 10);
}

The choice is yours. Calling toString method on the wrapper class directly can be slightly faster as you save on one method call, but as Jed rightly pointed out modern JVMs are very efficient and the improvement would be very minimal.

Eelco Hillenius said...

You should use String.valueOf even if you know the implementation today. The implementation might be optimized some day. Not that relevant in this concrete case maybe. But imo just better programming practice.


Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.