Source of StringValueOf.java


  1: // Fig. 29.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 = 'Z';
 11:       int integerValue = 7;
 12:       long longValue = 10000000000L; // L suffix indicates long
 13:       float floatValue = 2.5f; // f indicates that 2.5 is a float
 14:       double doubleValue = 33.333; // no suffix, double is default
 15:       Object objectRef = "hello"; // assign string to an Object reference
 16: 
 17:       System.out.printf( 
 18:          "char array = %s\n", String.valueOf( charArray ) );
 19:       System.out.printf( "part of char array = %s\n", 
 20:          String.valueOf( charArray, 3, 3 ) );
 21:       System.out.printf( 
 22:          "boolean = %s\n", String.valueOf( booleanValue ) );
 23:       System.out.printf( 
 24:          "char = %s\n", String.valueOf( characterValue ) );
 25:       System.out.printf( "int = %s\n", String.valueOf( integerValue ) );
 26:       System.out.printf( "long = %s\n", String.valueOf( longValue ) ); 
 27:       System.out.printf( "float = %s\n", String.valueOf( floatValue ) ); 
 28:       System.out.printf( 
 29:          "double = %s\n", String.valueOf( doubleValue ) ); 
 30:       System.out.printf( "Object = %s\n", String.valueOf( objectRef ) );
 31:    } // end main
 32: } // end class StringValueOf
 33: 
 34: /**************************************************************************
 35:  * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and               *
 36:  * Pearson Education, Inc. All Rights Reserved.                           *
 37:  *                                                                        *
 38:  * DISCLAIMER: The authors and publisher of this book have used their     *
 39:  * best efforts in preparing the book. These efforts include the          *
 40:  * development, research, and testing of the theories and programs        *
 41:  * to determine their effectiveness. The authors and publisher make       *
 42:  * no warranty of any kind, expressed or implied, with regard to these    *
 43:  * programs or to the documentation contained in these books. The authors *
 44:  * and publisher shall not be liable in any event for incidental or       *
 45:  * consequential damages in connection with, or arising out of, the       *
 46:  * furnishing, performance, or use of these programs.                     *
 47:  *************************************************************************/