Java String valueOf() method explained with examples
Java String valueOf() method returns the String representation of the boolean, char, char array, int, long, float and double arguments. We have different versions of this method for each type of arguments.
Different variants of java string valueOf() method
Java String valueOf() simple example
Lets take a simple example to understand the usage of this method. In this example we are concatenating the double nines to the end of the given value. The given value is an integer, in order to append 99 at the end of the integer we must need to convert the given integer to the string first. We are using valueOf() method to convert the number to the equivalent string str and then we are concatenating the 99 at the end of converted string.

Output:
Method valueOf() example 2
In this example, we are converting an array to a string using valueOf() method.

Output:
Java String valueOf() Example
Lets take an example, where we are using all the variants of valueOf() method. In this example we are using valueOf() method to convert the integer, float, long, double, char and char array to the String.
Output:
About the Author
I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.
Class String
Strings are constant; their values cannot be changed after they are created. String buffers support mutable strings. Because String objects are immutable they can be shared. For example:
is equivalent to:
Here are some more examples of how strings can be used:
The class String includes methods for examining individual characters of the sequence, for comparing strings, for searching strings, for extracting substrings, and for creating a copy of a string with all characters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standard version specified by the Character class.
The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. For additional information on string concatenation and conversion, see The Java Language Specification.
Unless otherwise noted, passing a null argument to a constructor or method in this class will cause a NullPointerException to be thrown.
A String represents a string in the UTF-16 format in which supplementary characters are represented by surrogate pairs (see the section Unicode Character Representations in the Character class for more information). Index values refer to char code units, so a supplementary character uses two positions in a String .
The String class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., char values).
Unless otherwise noted, methods for comparing Strings do not take locale into account. The Collator class provides methods for finer-grain, locale-sensitive String comparison.
String valueOf() in Java
String valueOf() method is present in String class of java.lang package. valueOf() in Java is used to convert any non String variable or Object such as int , double , char , and others to a newly created String object. It returns the string representation of the argument passed.
Syntax of valueOf() in Java
The following two signatures are possible:
The first signature takes in a data type like int and returns a String object. The other possible data types of T are explained in the parameters section.
The second signature takes in a character array and two other integers, which we will discuss in the parameters section.
The syntax of valueOf() is as follows:
Parameters of valueOf() in Java
As per first method signature, T can be:
- boolean
- int
- long
- float
- double
- char
- char[]
- Object
For the second method-signature parameters are char array, offset and count.
- offset is the starting index in the case of a char array
- count is the number of characters to be considered in the sub-array starting with offset
Return Values of valueOf() in Java
The return type is a String class object.
Exceptions of valueOf() in Java
The only exception valueOf() can throw is IndexOutOfBoundsException in the second signature if:
- offset is negative
- count is negative
- count + offset is greater than the length of the char array
If, instead of int some other data type is passed, or the number of parameters is less or more than expected, we will get a compile-time error.
Example of valueof() in Java
Output:
What is valueOf() in Java?
String valueOf() is very useful in cases where we want to concatenate non-string variables.
For example, consider a function that gives us the version of a file in x.y format where x is the major version and y is the minor version.
Imagine a Version class as follows:
We can use valueOf() operator to get the concatenated version as follows:
Output:
How does valueOf() work?
valueOf() works by invoking the toString() method of the respective data type passed to valueOf() .
Thus any object can be passed to valueOf() , and we would not get any error, as internally, toString() method would get invoked on that object.
Note: toString() method is declared in the Object class and every class in Java inherits the Object class.
Example 1: Java String valueOf() for Numbers
Output:
Example 2: Convert Char Array to String
To understand the conversion, let us look at the example given below. Here, a character array is converted to a string.
Output:
Example 3: Convert Char Subarray to String
To convert subarray of char array, we use the second signature of valueOf which takes in an offset and length.
Java — String valueOf() Method
This method has the following variants, which depend on the passed parameters. This method returns the string representation of the passed argument.
valueOf(boolean b) − Returns the string representation of the boolean argument.
valueOf(char c) − Returns the string representation of the char argument.
valueOf(char[] data) − Returns the string representation of the char array argument.
valueOf(char[] data, int offset, int count) − Returns the string representation of a specific subarray of the char array argument.
valueOf(double d) − Returns the string representation of the double argument.
valueOf(float f) − Returns the string representation of the float argument.
valueOf(int i) − Returns the string representation of the int argument.
valueOf(long l) − Returns the string representation of the long argument.
valueOf(Object obj) − Returns the string representation of the Object argument.