Converting Primitives to Strings

Just as you can convert an Object representation into a String, you can also explicitly convert a primitive into a String. There are several ways that this can be done, but by far the easiest is to use the String class's valueOf() method. The valueOf method can be used to convert just about any type of data object to a String, thus enabling you to display the Object's value onscreen. The following lines convert an integer to a string, for example:

int value = 10;
String str = String.valueOf( value );

Notice that valueOf is a static method, meaning that it can be called by referencing the String class directly, without having to instantiate a String object. Of course, you can also call valueOf through any object ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.