Data Conversion Using valueOf()


The ValueOf() method converts data from an internal format to a human-readable form. This is a method that is overloaded into strings for all built-in types of Java so that each type can be converted to a string correctly. The valueOf() object is also overloaded for typing, so an object of any kind of class you create can also be used as an argument.

static String valueOf(double num)
static String valueOf(long num)
static String valueOf(Object obj)
static String valueOf(char chars[])

valueOf() is called when a string representation of some other type of data is required - for example, during a concentration operation. You can call this method directly with any data type and get a reasonable string representation. All of the simple types are converted to their string representations. Any object that you pass to the valueOf() will return the result of a call to the object toString() method. In fact, you can simply call the toString() directly and get the same result.

For more arrays, the valueOf() returns a rather cryptic string, indicating that it is an array of some kind. For variable arrays, however, a string object is created that contains the characters in the variable array. There is a special version of valueOf() that lets you specify a subset of a variable array. It has this simple form:

static String valueOf(char chars[], int startIndex, int numChars);

Here, chars are the array that holds the characters, startIndex is the index into the array of characters at which the desired substring begins, and numChars specifies the length of the substring.