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 of the String class, like this:

 int value = 10; String str1 = ""; String str2 = str1.valueOf( value ); 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net