Java String valueOf()

The String.valueOf() method in Java is a multipurpose static method. Its major function lies in the conversion of types of data, such as primitive types and objects, into strings. The technique provides an efficient and convenient way to construct string objects from different sources.

Java's valueOf() method is overloaded with numerous forms, each of that is designed to handle specific data types. It can handle converting boolean, char, char array, double, float, int, long, and Object types into their respective string representations.

With the help of valueOf(), developers can get back string representations of variables and objects without the need to concatenate or modify strings directly. It simplifies the code and enhances readability by offering a cleaner option instead of string conversion with the hands.

Internal implementation

Method Signature

The signature or syntax of string valueOf() method is given below:

Returns

String representation of a given value.

Java String valueOf() Method Example

StringValueOfExample.java

Output:

3010

Java String valueOf(boolean bol) Method Example

This is a boolean version of overloaded valueOf() method. It takes boolean value and returns a string. Let's see an example.

StringValueOfExample.java

Output:

The boolean value is: true
String representation using valueOf: true 

Java String valueOf(char ch) Method Example

This is a char version of overloaded valueOf() method. It takes char value and returns a string. Let's see an example.

StringValueOfExample.java

Output:

The char value is: A
String representation using valueOf: A 

Java String valueOf(float f) and valueOf(double d) Method

It is a float version of the overloaded valueOf() method. It takes a float value and returns a string. Let's see an example.

StringValueOfExample.java

Test it Now

Output:

Float value and its String representation: 123.45 -> 123.45
Double value and its String representation: 987.654 -> 987.654

Java String valueOf() Complete Examples

Let's see an example where we are converting all primitives and objects into strings.

StringValueOfExample.java

Output:

Boolean: true
Char: A
Int: 42
Long: 123456789
Float: 123.45
Double: 987.654
Integer Object: 123
Double Object: 456.789