14.3.8 String Method valueOf

As we’ve seen, every object in Java has a toString method that enables a program to obtain the object’s string representation. Unfortunately, this technique cannot be used with primitive types because they do not have methods. Class String provides static methods that take an argument of any type and convert it to a String object. Figure 14.9 demonstrates the String class valueOf methods.

 1   // Fig. 14.9: StringValueOf.java 2   // String valueOf methods. 3  4   public class StringValueOf 5   { 6      public static void main(String[] args) 7      { 8         char[] charArray = {'a', 'b', 'c', 'd', 'e', 'f'}; 9         boolean booleanValue = true;10         char characterValue =

Get Java™ How To Program (Early Objects), Tenth 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.