Thursday, 4 December 2014

// The "StringMethodsExample8" class.  Page 357-358.
// (from the Carter book)
// Example 8 and 9
//
// Example 8:
//
// Values of any type can be converted into Strings.  To
// convert any object into a String object, we can use the
// String class' "toString" method.  To convert a value in
// any of Java's primitive data types to a String, we can
// use the method in the String class called "valueOf".
// This method is also known as "overloaded" so that its
// parameter can be of any type.
//
// Example 9:
//
// The following are uses of the "valueOf" method, as it
// used on types like int, char and double / float.  Notice
// that a String object was NOT created first in order for
// us to use this method:

public class StringMethodsExample8and9
{
    public static void main (String[] args)
    {
        // 9(a):
        System.out.println (String.valueOf (123));    
        // returns "123" (a String made up of an int)

        // 9(b):
        System.out.println (String.valueOf ('a'));    
        // returns "a"   (a String made up of 1 character)

        // 9(c):
        System.out.println (String.valueOf (2.5));    
       // returns "2.5"  (a String made up of a float)

    } // main method
} // StringMethodsExample8and9 class

No comments:

Post a Comment