exampracticeonline.com
String valueOf()

Java String valueOf()

The java string valueOf() method converts different data type values into string data types. By the use of string valueOf() method, you can change over 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.

Internal Method syntax

public static String valueOf(Object obj) {  
       return (obj == null) ? "null" : obj.toString();  
}  

String valueOf() method is overloaded and accept following types of arguments:

  1. int
  2. long
  3. float
  4. double
  5. char
  6. char array
  7. boolean
  8. Object

Method Syntax

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

1. int to String

public static String valueOf(int i) {
        return Integer.toString(i);
}

2. long to String

public static String valueOf(long l) {
        return Long.toString(l);
}

3. float to String

public static String valueOf(float f) {
        return Float.toString(f);
}

4. double to String

public static String valueOf(double d) {
        return Double.toString(d);
}

5. char to string

public static String valueOf(char c) {
        char data[] = {c};
        return new String(data, true);
}

6. char array to string

public static String valueOf(char data[]) {
        return new String(data);
}

7. boolean to string 

public static String valueOf(boolean b) {
        return b ? "true" : "false";
}

8. object to string

public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
}