Java String valueOf() Method

The java string valueOf() function transforms various value types into strings. Using the string valueOf() method, we can convert int to string, long to string, boolean to string, character to string, float to string, double to string, object to string, and char array to string.

Explanation

The Java valueOf method can transform different types of values into strings. This method can convert a variety of data types to strings, including Boolean, char, char array, double, integer, float, long, and short.

Syntax

The syntax of the String valueOf() function for various data types is shown below.

String.valueOf(boolean b)
String.valueOf(char c)
String.valueOf(char[] data)
String.valueOf(double d)
String.valueOf(float f)
String.valueOf(int b)
String.valueOf(long l)
String.valueOf(Object o)
Function Description
valueOf(boolean b) Returns a string containing the boolean argument’s representation.
valueOf(char c) Returns a string containing the char argument’s representation.
valueOf(char[] data) Returns a string containing the char array argument’s representation.
valueOf(double d) Returns the double argument’s string representation.
valueOf(float f) Returns the float argument’s string representation.
valueOf(int b) Returns the int argument’s string representation.
ValueOf(long)   Returns the long argument’s string representation.
valueOf(Object o) Returns the Object argument’s string representation.

Parameter
The valueOf() method only accepts one parameter.the data to be turned into a string

Return value
It returns the string representation of the supplied argument.

ValueOf()
This method returns the string’s primitive value.
This technique makes no changes to the original string.
This function helps convert a string object to a string.

Diagram

java-String-valueOf()-diagram

Example

class Main 
{
  public static void main(String[] args)
 {
    double interest = 923.443d;
       System.out.println(String.valueOf(interest));

  }
}

Output
923.443

Example

public class StringValueOf
 {  
    public static void main(String[] args) 
{         
        boolean bol = True;    
        boolean bol2 = False;    
        String s1 = String.valueOf(bol);    
        String s2 = String.valueOf(bol2);  
        System.out.println(s1);  
        System.out.println(s2); 
    }  
}

Output
True
False

Example

public class StringValueOf
 {  
    public static void main(String[] args)
 {  
                
        char ch1 = 'J';    
        char ch2 = 'A';  
        String s1 = String.valueOf(ch1);    
        String s2 = String.valueOf(ch2);  
        System.out.println(s1);  
        System.out.println(s2);  
    }  
}

Output
J
A

Difference between value() and valueOf() method in Java

Value() ValueOf ()
The values() method produces an array containing a list of enumeration constants. The valueOf() method returns the enumeration constant whose value matches the string str.

Conclusion

The static function valueOf() is found in the String class. It creates a new String object from any data type, such as int, double, char, char array, etc.