valueOf() in Java

By: Emiley J Emailed: 1769 times Printed: 2515 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

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.


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(2)


1. View Comment

its nice explanation.

View Tutorial          By: rahul ahuja at 2010-06-13 22:33:35
2. View Comment

I have this line of code but i don't understand what is meant to do:
String line = "Some data";
int size = -1;
size = Integer.valueOf(line, 16).intValue();

Would you please explain to me what it does in relation to what you said above? Thanx


View Tutorial          By: Xris at 2011-11-08 06:01:01

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Emiley J
Password must include both numeric and alphabetic characters - Magento
What is Hadoop?
Returning multiple values from a web service
Tomcat and httpd configured in port 8080 and 80
Java Webservices using Netbeans and Tomcat
Java WebService connected to Database
How to Deploy a Java Web Service
Call a webservice in Java
Java WebService - Create your first web service in Java
package javax.jws does not exist
Getting Started with Android
HTML5 Location - getCurrentPosition() in HTML5
HTML5 Canvas - Using Canvas in HTML5
HTML5 - Introduction
HTML5 Video - Handling video in HTML5

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to use ArrayList in Java
Using One-Dimensional Arrays in Java
FileReader and FileWriter example program in Java
Method Overloading (function overloading) in Java
compareTo( ) in Java
InetAddress Example program in Java
Student marks calculation program in Java
indexOf( ) and lastIndexOf( ) in Java
Calculate gross salary in Java
How to Send SMS using Java Program (full code sample included)
Read from a COM port using Java program
append() in Java
Simple Port Scanner application using Java
XML and Java - Parsing XML using Java Tutorial
Vector in Java - Sample Program
Most Emailed Articles (in Java)
Using Charts in JasperReports
Introducing Nested and Inner Classes
Increment and Decrement Operator
Tomcat and httpd configured in port 8080 and 80
Convert numbers to word as per Indian number system in java
List all the serial and parallel ports in your system using Java
What is Unicode?
StreamTokenizer sample program in Java
FileReader and FileWriter example program in Java
FilenameFilter - sample program in Java
Transient vs Volatile modifiers in Java
Basics of Inheritance in Java
Method Overloading (function overloading) in Java
while - Iteration in java
how to use boolean data type in Java