By: Emiley J in Java Tutorials on 2007-09-02
The valueOf() method converts data from its internal format into a human-readable form. It is a static method that is overloaded within String for all of Java's built-in types, so that each type can be converted properly into a string. valueOf() is also overloaded for type Object, so an object of any class type you create can also be used as an argument. (Recall that Object is a superclass for all classes.)
Here are a few of its forms:
static String valueOf(double num)
static String valueOf(long num)
static String valueOf(Object ob)
static String valueOf(char chars[ ])
As we discussed earlier, valueOf() is called when a string representation of some other type of data is neededâ€”for example, during concatenation operations. 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 common String representation. Any object that you pass to valueOf() will return the result of a call to the object's toString() method. In fact, you could just call toString() directly and get the same result.
For most arrays, valueOf() returns a rather cryptic string, which indicates that it is an array of some type. For arrays of char, however, a String object is created that contains the characters in the char array. There is a special version of valueOf() that allows you to specify a subset of a char array. It has this general form:
static String valueOf(char chars[ ], int startIndex, int numChars)
Here, chars is 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.
// Using valueOf() method for Integer class int i = 100; Integer intObj = Integer.valueOf(i); // returns an Integer object representing the value of i // Print the value of Integer object System.out.println("Value of Integer object: " + intObj); // Output: Value of Integer object: 100
In this example, we first declare an
i and initialize it to
100. We then use the
valueOf() method of the
Integer class to convert the
int value to an
We can also use the
valueOf() method for other primitive types such as
Boolean. For example, here's how we can use
valueOf() for the
// Using valueOf() method for Double class double d = 3.14159; Double doubleObj = Double.valueOf(d); // returns a Double object representing the value of d // Print the value of Double object System.out.println("Value of Double object: " + doubleObj); // Output: Value of Double object: 3.14159
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Java )
Latest Articles (in Java)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews